System.Diagnostics.Process调用批处理.bat
using System;
class Program
{
void RunServer(string path, string fileName)
{
path = string.Format(path);
try
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
//proc.StartInfo.WorkingDirectory = path; // 初始化可执行文件的文件夹信息 ===== 建议路径写到filename中,写这里可能引起 Win32Exception
proc.StartInfo.FileName = path+fileName;// 初始化可执行文件名
Console.WriteLine("name: " + proc.StartInfo.FileName);
proc.StartInfo.Arguments = ""; //this is argument
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
//proc.StartInfo.CreateNoWindow = true;//不创建窗口
//proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;//设置DOS窗口不显示,
Console.WriteLine("fileExists: " + System.IO.File.Exists(path + fileName));
proc.Start();
proc.WaitForExit();//服务开启时间(WaitForExit(1000毫秒)),不传则无限
if (proc.Start())
Console.WriteLine("服务启动成功。");
proc.Close();
}
catch (System.Exception e)
{
Debug.Log("服务启动失败:" + e);
throw;
}
}
static void Main(string[] args)
{
string path = @"C:\Users\uinnova\Desktop\Debugger\";
Program p = new Program();
p.RunServer(path, "run.bat");
Console.ReadKey();
}
}