当前位置: 编程技术>.net/c#/asp.net
c# 调用批处理(bat)的实现代码
来源: 互联网 发布时间:2014-08-30
本文导语: c#调用批处理文件。 代码示例: static void Main(string[] args) { Process proc = null; try { string targetDir = string.Format(@"D:myfilessetup");//this is where mybatch.bat lies proc = new Process(); proc.StartInfo.WorkingDirectory = targetDir; proc.StartInfo.FileName = "mybat...
c#调用批处理文件。
代码示例:
static void Main(string[] args)
{
Process proc = null;
try
{
string targetDir = string.Format(@"D:myfilessetup");//this is where mybatch.bat lies
proc = new Process();
proc.StartInfo.WorkingDirectory = targetDir;
proc.StartInfo.FileName = "mybatch.bat";
proc.StartInfo.Arguments = string.Format("10");//this is argument
proc.StartInfo.CreateNoWindow = false;
proc.Start();
proc.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine("Exception Occurred :{0},{1}", ex.Message,ex.StackTrace.ToString());
}
}
{
Process proc = null;
try
{
string targetDir = string.Format(@"D:myfilessetup");//this is where mybatch.bat lies
proc = new Process();
proc.StartInfo.WorkingDirectory = targetDir;
proc.StartInfo.FileName = "mybatch.bat";
proc.StartInfo.Arguments = string.Format("10");//this is argument
proc.StartInfo.CreateNoWindow = false;
proc.Start();
proc.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine("Exception Occurred :{0},{1}", ex.Message,ex.StackTrace.ToString());
}
}
如果要运行时隐藏dos窗口,需使用下面的代码
代码示例:
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.CreateNoWindow = true;