1. 程式人生 > >九、將cs文件快速的轉換成可執行文件和響應文件(配置編譯開關的文件)

九、將cs文件快速的轉換成可執行文件和響應文件(配置編譯開關的文件)

文本文件 font OS reference 方便 sha 文本文 libraries rgs

1、將包含多個類型的源代碼文件轉換為可以部署的文件。有如下Program.cs的文件,代碼如下:

    public sealed class Program
    {
        public static void Main(string[] args)
        {
            System.Console.WriteLine("Hello World");
        System.Console.ReadKey();
        }
    }

該應用程序實現了打印"Hello World"的功能,該應用程序中引用到的系統類型都來自於MSCorLib.dll文件,簡言之,該應用程序定義了一個類型,該類型使用到了其他公司提供的類型.

下面通過命令行來快速將該文件生成為可執行的文件

第一步:打開命令行,輸入以下命令,定位到csc.exe文件所在目錄中(Win10下)

cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319

第二步:執行以下命令

csc.exe /out:C:\Users\zc\Desktop\Program.exe /t:exe /r:MSCorLib.dll C:\Users\zc\Desktop\Program.cs

參數解析:

1、csc.exe 編譯器開關

2、/out:C:\Users\zc\Desktop\Program.exe 輸出文件的位置和輸出文件的類型

3、/t(target):exe 生成的文件是Win32控制臺應用程序類型

4、C:\Users\zc\Desktop\Program.cs 輸入文件的位置

5、/r(reference):MSCorLib.dll 編譯文件時需要引用的程序集

因為Console類型使框架定義的類型,所以必須指定其所在的dll文件,所以添加了/r(reference):MSCorLib.dll開關,告訴編譯器在MSCorLib.dll中查找引用的外部類型.

因為MSCorLib.dll是特殊文件,它包含幾乎所有的核心類型,所以C#編譯器會默認引用該類型,所以.命令可以簡化成如下:

csc.exe /out:C:\Users\zc\Desktop\Program.exe /t:exeC:\Users\zc\Desktop\Program.cs

還有,由於/out:C:\Users\zc\Desktop\Program.exe和/t:exe是默認設定,所以命令可以簡化成如下:

csc.exe  C:\Users\zc\Desktop\Program.cs

技術分享圖片

2、響應文件

響應文件是包含一組命令行開關的文本文件.執行cse.exe時,編譯器打開響應文件,並使用其中包含的所有的開關,就是通過文件的形式一次性將所有的開關都傳遞給命令行,在命令行中,在@符號後面指定響應文件的名稱.

響應文件代碼如下:

/out:Test.exe
/t:exe

class1.cs文件如下:

    public class Class1
    {
        public static void Main(string[] args)
        {
            System.Console.WriteLine(Class2.A);
        System.Console.ReadKey();
        }
    }

class2.cs文件如下:

    public class Class2
    {
        public static string A {get{return "111";}} 
    }

命令行代碼如下:

csc.exe @test.rsp C:\Users\zc\Desktop\Class1.cs C:\Users\zc\Desktop\Class2.cs

技術分享圖片

註:rsp文件必須和csc.exe同文件夾

技術分享圖片

通過上面的例子可以看出響應文件帶給我們的便利性,不用手動輸入命令行中參數。

重點:除了顯示指定的響應文件,編譯器還會自動查找名為csc.rsp的相應文件,如果自定義的響應文件和本地響應文件發生沖突,則本地的為主.

在安裝.Net FrameWork時會自動安裝csc.rsp文件,該文件強制編譯時需要執行的命令行開關,代碼如下:

# This file contains command-line options that the C#
# command line compiler (CSC) will process as part
# of every compilation, unless the "/noconfig" option
# is specified. 

# Reference the common Framework libraries
/r:Accessibility.dll
/r:Microsoft.CSharp.dll
/r:System.Configuration.dll
/r:System.Configuration.Install.dll
/r:System.Core.dll
/r:System.Data.dll
/r:System.Data.DataSetExtensions.dll
/r:System.Data.Linq.dll
/r:System.Data.OracleClient.dll
/r:System.Deployment.dll
/r:System.Design.dll
/r:System.DirectoryServices.dll
/r:System.dll
/r:System.Drawing.Design.dll
/r:System.Drawing.dll
/r:System.EnterpriseServices.dll
/r:System.Management.dll
/r:System.Messaging.dll
/r:System.Runtime.Remoting.dll
/r:System.Runtime.Serialization.dll
/r:System.Runtime.Serialization.Formatters.Soap.dll
/r:System.Security.dll
/r:System.ServiceModel.dll
/r:System.ServiceModel.Web.dll
/r:System.ServiceProcess.dll
/r:System.Transactions.dll
/r:System.Web.dll
/r:System.Web.Extensions.Design.dll
/r:System.Web.Extensions.dll
/r:System.Web.Mobile.dll
/r:System.Web.RegularExpressions.dll
/r:System.Web.Services.dll
/r:System.Windows.Forms.Dll
/r:System.Workflow.Activities.dll
/r:System.Workflow.ComponentModel.dll
/r:System.Workflow.Runtime.dll
/r:System.Xml.dll
/r:System.Xml.Linq.dll

該文件幫助我們引入一些基礎的dll程序集,方便我們不用每次使用時都要輸入對應的命令行開關.

技術分享圖片

九、將cs文件快速的轉換成可執行文件和響應文件(配置編譯開關的文件)