以下列出两种方法:
方法一,使用win api:
方法一,使用win api:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace CSCTest
{
public enum ShowCommands
{
Hide = 0,
ShowNormal = 1,
ShowMinimized = 2,
ShowMaximized = 3,
Maximize = 3,
ShowNormalNoActivate = 4,
Show = 5,
Minimize = 6,
ShowMinNoActivate = 7,
ShowNoActivate = 8,
Restore = 9,
ShowDefault = 10,
ForceMinimized = 11
}
public class WindowsAPI
{
[DllImport("shell32.dll")]
public static extern IntPtr ShellExecute(
IntPtr hwnd,
string lpOperation,
string lpFile,
string lpParameters,
string lpDirectory,
ShowCommands nShowCmd);
}
class Program
{
static void Main(string[] args)
{
//string strCMD = @"F:\cscTest\complier.bat";
WindowsAPI.ShellExecute(IntPtr.Zero, "open", strCMD, "", "", ShowCommands.Hide);
Console.ReadLine();
}
}
}
cd F:\csctest
F:
csc.exe /out:SayHello.exe /target:winexe Hello.cs /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client\PresentationFramework.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client\WindowsBase.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client\PresentationCore.dll"
Hello.cs的内容如下
using System;
using System.Windows;
namespace zzm.SayHello
{
class SayHello
{
[STAThread]
public static void Main()
{
Window win = new Window();
win.Height = 200;
win.Width = 300;
win.Title = "你好,WPF!";
win.Show();
Application app = new Application();
app.Run();
}
}
}
方法二,使用C#的CMD:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace CSCTest
{
class Program
{
static void Main(string[] args)
{
MyBatCommand();
Console.ReadLine();
}
public static void MyBatCommand()//名称
{
//如下的三个字符串,代表三条批处理命令
string MyDosComLine1, MyDosComLine2, MyDosComLine3;
const string cscCmd = "csc.exe /out:SayHello.exe /target:winexe Hello.cs ";
const string rpath1 = @"/reference:""C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client\PresentationFramework.dll"" ";
const string rpath2 = @"/reference:""C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client\WindowsBase.dll"" ";
const string rpath3 = @"/reference:""C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client\PresentationCore.dll""";
MyDosComLine1 = @"cd F:\csctest";//返回根目录命令
MyDosComLine2 = "F:";
MyDosComLine3 = cscCmd + rpath1 + rpath2 + rpath3;//将当前目录所有文件复制粘贴到E盘
Process myProcess = new Process();
myProcess.StartInfo.FileName = "cmd.exe ";//打开DOS控制平台
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = false;//是否显示DOS窗口,true代表隐藏;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.Start();
StreamWriter sIn = myProcess.StandardInput;//标准输入流
sIn.AutoFlush = true;
StreamReader sOut = myProcess.StandardOutput;//标准输入流
StreamReader sErr = myProcess.StandardError;//标准错误流
sIn.Write(MyDosComLine1 + System.Environment.NewLine);//第一条DOS命令
sIn.Write(MyDosComLine2 + System.Environment.NewLine);//第二条DOS命令
sIn.Write(MyDosComLine3 + System.Environment.NewLine);//第三条DOS命令
sIn.Write("exit" + System.Environment.NewLine);//第四条DOS命令,退出DOS窗口
string s = sOut.ReadToEnd();//读取执行DOS命令后输出信息
string er = sErr.ReadToEnd();//读取执行DOS命令后错误信息
Console.WriteLine(s);
Console.WriteLine(er);
if (myProcess.HasExited == false)
{
myProcess.Kill();
//MessageBox.Show(er);
}
else
{
//MessageBox.Show(s);
}
sIn.Close();
sOut.Close();
sErr.Close();
myProcess.Close();
}
}
}