九、将cs文件快速的转换成可执行文件和响应文件(配置编译开关的文件)

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程序集,方便我们不用每次使用时都要输入对应的命令行开关.

 

 

posted @ 2018-02-27 18:11  郑小超  阅读(1523)  评论(0编辑  收藏  举报