| 編譯 File.cs 以產(chǎn)生 File.exe: csc File.cs 編譯 File.cs 以產(chǎn)生 File.dll: csc /target:library File.cs? 可縮寫成 csc /t:library File.cs? 編譯 File.cs 并創(chuàng)建 My.exe: csc /out:My.exe File.cs 默認生成的文件夾路徑是C:\Program Files\Microsoft Visual Studio 10.0\VC 通過使用優(yōu)化和定義 DEBUG 符號,編譯當前目錄中所有的 C# 文件。輸出為 File2.exe: csc /define:DEBUG /optimize /out:File2.exe *.cs 編譯當前目錄中所有的 C# 文件,以產(chǎn)生 File2.dll 的調試版本。不顯示任何徽標和警告: csc /target:library /out:File2.dll /warn:0 /nologo /debug *.cs 將當前目錄中所有的 C# 文件編譯為 Something.xyz(一個 DLL): csc /target:library /out:Something.xyz *.cs 編譯 File.cs 以產(chǎn)生 File.dll: csc /target:library File.cs這個就是我們使用最多的一個命令,其實可以簡單的寫成csc /t:library File.cs,另外的一個寫法是 csc /out:mycodebehind.dll /t:library mycodebehind.cs,這個可以自己指定輸出的文件名。 csc /out:mycodebehind.dll /t:library mycodebehind.cs mycodebehind2.cs,這個的作用是把兩個cs文件裝到一個.dll文件里,很有用哦。 首先要安裝好.NET Framwork SDK環(huán)境,一般安裝了vs.net工具的 都已經(jīng)可以了. 首先,我們用文本文件建一個.cs的文件 其實這個擴展名是可以自己定的,另不用.cs也是可以的,建一個文件命名為:SimpleProgram.cs 輸入下面的代碼 using System; publicclass SimpleProgram { ????staticvoid Main() ????{ ???????? Console.WriteLine(“Hello”); ???????? Console.ReadLine(); ???? } } 保存,注意一下大小寫,然后在命令行窗口里輸入:csc SimpleProgram.cs,當然我們要首先進入文件所在目錄 如果編譯通過,在同一個文件夾里會出現(xiàn)一個SimpleProgram.exe,運行 就可以看到一個控制臺程序 打印出Hello. 接著我們講編譯多個源文件 編譯多個源文件有幾種方面:1.把所有的源文件編譯成一個exe文件 2.把一些文件編譯成dll,一些編譯成exe 然后作為一個應用程序一起使用。 先說第一個方法:做兩個.cs文件 Program2.cs using System publicclass Program2 { ????staticvoid Main() ????{ ???????? Person p=new Person(“dotLive”); ???????? Console.WriteLine(“Our person’s nameis “+p.Name); ???? } } Person.cs using System; publicclass Person { ????privatestring name; ????public Person(string s) ????{ ???????? name=s; ???? } ????publicstring Name ????{ ????????get{ ????????????return name; ???????? } ????????set ????????{ ???????????? name=value; ???????? }???? ???? } ?? } 保存,命令行窗口中進入文件目錄 輸入:csc Program2.cs Person.cs?? 這個時候會在目錄里生成一個exe文件,文件的名稱跟第一個.cs的文件名相同,如果.cs文件很多的話 還有一個簡單的寫法:csc *.cs 就可以把當前目錄里的所有.cs文件進行編譯。 現(xiàn)在說一下第二種方法,還是使用上面兩個.cs文件 首先輸入 csc /t:library Person.cs命令 這時候會生成一個Person.dll文件,然后第二步輸入命令csc Program2.cs /r:Person.dll這樣就可以生成一個引用Person.dll的exe文件Program2.exe文件。 這里出現(xiàn)了兩個參數(shù) /t 和 /r /t 是指定編譯生成dll還是exe,在第一種方法中的命令csc *.cs 其實是一個簡寫,完整的寫法是csc /t:exe *.cs,由于/t:exe是默認的選項可以不寫, 還要說一句 這里的exe文件都是控制臺應用程序(其他的以后再說) /r 的作用可以看成是引用了一個dll文件,格式可以寫成csc program.cs /r:1.dll;2.dll;3.dll 掛接多個dll,每個dll之間用分號 隔開。 接著我們再講一個參數(shù) /out 這個是給可執(zhí)行文件命名的 比如:csc /out:MyApp.exe Program2.cs Person.cs 這樣就可以生成一個叫做MyApp.exe的可執(zhí)行文件。 最后再將一個 如果有多個.cs文件中存在Main函數(shù)要怎么辦? 首先我們在Person.cs文件的Person類里添加一個Main函數(shù) using System; publicclass Person { ????privatestring name; ????public Person(string s) ????{ ???????? name=s; ???? } ????publicstring Name ????{ ????????get{ ????????????return name; ???????? } ????????set ????????{ ???????????? name=value; ???????? }???? ???? } ????//新增加一個Main函數(shù) ????staticvoid Main() ????{ ???????? Person p=new Person(“dotLive2”); ???????? Console.WriteLine(“Our person’s nameis “+p.Name); ???? } ?? } 這樣一來如果按照原來的方法同時將兩個文件編譯進同一個exe時,便以一定會出錯 這時候我們再引入一個新的參數(shù) /main 在我們自己決定要使用哪個cs文件里的Main函數(shù)時可以輸入命令 csc Program2.cs Person.cs /main Program2 就是制定了以Program2里的Main函數(shù)為程序入口點了。 |