馬上就要過年了,每年過年都要回老家,使用電腦和網絡就沒有這邊這么方便了。想寫程序了,都沒有一台帶Visual Studio的機器,我也不可能給每台機器都安裝一個Visual Studio,怎么辦呢?
網上搜索了一下,不是說需要Visual Studio的就是說需要.net framework sdk,Visual Studio好幾個G,這個sdk也不小,1.3G,我的優盤沒法裝(優盤大小只有1G L)
SDK其實有很多我不需要的東東,比如文檔,其實我需要的只是一個編譯器而已,於是我嘗試着把csc.exe拷到自己的優盤上,然后拷到老爸的機器上,用控制台一執行,根本無法使用!!一檢查,原來他的機器根本沒有安裝.net framework
於是我就去微軟的主頁上找到.net framework 3.5 sp1的下載鏈接(不是SDK)(http://download.microsoft.com/download/2/0/e/20e90413-712f-438c-988e-fdaa79a8ac3d/dotnetfx35.exe),這個還比較好,只有231M,下載安裝完畢,發現原來壓根就不用自己去拷csc.exe了,在C:/WINDOWS/Microsoft.NET/Framework/v3.5下已經有csc.exe了。哈哈!這樣一切准備齊全:有編輯器(Windows自帶的記事本),有編譯器(csc.exe),也有運行環境(.net framework)
那么怎么來使用csc.exe編譯寫好的程序呢?
我們可以寫一個程序App.cs
using System;
using System.Windows.Forms;
namespace MyApplication
{
class App
{
public static void Main()
{
Application.Run(new Form());
}
}
}
保存在C盤下。然后調出控制台(開始菜單->運行->cmd,回車),在當前位置為C盤的情況下,輸入cd C:/WINDOWS/Microsoft.NET/Framework/v3.5,進入該文件夾
輸入csc /t:winexe /r:System.dll /r:System.Windows.Forms.dll /out:C:/app.exe C:/App.cs 回車
就能在C盤下看到有app.exe的文件了,點擊運行,就能看到Form了。
這里/t的含義是編譯得到的文件類型,可以有4種:exe,winexe,library和module。exe表示控制台運行程序,winexe表示windows運行程序,librar為dll,module為module文件。
/r是程序引用的dll,這里我需要引用兩個dll:System.dll和System.Windows.Forms.dll,由於這兩個dll是.net提供的,所以無需指定路徑,如果是非系統注冊的dll,就需要指定路徑了。
/out是指定輸出的文件,如果不指定,編譯生成的文件會默認保存到csc.exe所在的文件夾下
下面我們再寫兩個文件,將它們編譯成一個dll供App.exe調用
Test1.cs
using System;
namespace TestDLL
{
public class Test1
{
public String Text
{
get { return m_Text; }
set { m_Text = value; }
}
private String m_Text;
}
}
Test2.cs
using System;
namespace TestDLL
{
public class Test2
{
public Int32 Value
{
get { return m_Value; }
set { m_Value = value; }
}
private Int32 m_Value;
}
}
使用csc編譯成Test.dll
csc /t:library /r:System.dll /out:C:/Test.dll C:/Test1.cs C:/Test2.cs
然后修改App.cs為
using System;
using System.Windows.Forms;
using TestDLL;
namespace MyApplication
{
class App
{
public static void Main()
{
Test1 test1 = new Test1();
test1.Text = "This is my test form";
Test2 test2 = new Test2();
test2.Value = 100;
Form f = new Form();
f.Text = test1.Text;
f.Height = test2.Value;
Application.Run(f);
}
}
}
使用csc編譯為app.exe
csc /t:winexe /r:System.dll /r:System.Windows.Forms.dll /r:C:/Test.dll /out:C:/App.exe C:/App.cs
運行生成的app.exe文件,得到的結果如下