突然心血来潮,想搞搞C#。于是就先配个简单的命令行编译器吧。
1.添加环境变量 ,“C:/Windows/Microsoft.NET/Framework/v2.0.50727”。可以通过csc /?来查看参数。
2.通过csc命令行编译器来编译C#文件,以下为一个例子
1)在D盘下新建一个名为test.txt文本文件,输入以下文本后保存为test.cs文件
//一个简单的C#应用程序.
using System;
class TestApp
{
public static void Main()
{
Console.WriteLine("Hello world!");
Console.ReadKey();
}
}
2)运行 —〉cmd —〉D:/ —〉csc D:/test.cs , 编译成功后在D盘下生成test.exe可执行文件,运行即可。
3.通过引入System.Windows.Forms命名空间来生成Windows Forms程序 ,test.cs
修改为如下后再次编译
using System;
//一定要加上下面一行
using System.Windows.Forms;
class TestApp
{
public static void Main()
{
Console.WriteLine("Test! 1,2,3");
MessageBox.Show("Hello...","Application");
Console.ReadKey();
}
}
4.使用csc.exe编译多个源文件
//HelloMessage.cs
using System;
using System.Windows.Forms;
class HelloMessage
{
public void Speak()
{
MessageBox.Show("Hello");
}
}
//Test.cs
using System;
class TestApp
{
public static void Main()
{
Console.WriteLine("Testing! 1,2,3");
HelloMessage h = new HelloMessage();
h.Speak();
}
}
参数/out:编译结果的存放位置和名称
csc/out:e:/a.exe test.cs HelloMessage.cs
或者编译当前目录下的所有cs文件: csc/out:e:/a *cs
参考文章:学习C#之旅(配置C#命令行编译器)