关于C#运行文件的问题~~~

来源:百度知道 编辑:UC知道 时间:2024/06/15 05:32:11
我定义了
string s="a.bat";
string i = Application.ExecutablePath;
s = i + s;

也就是EXE相同目录的一个a.bat文件.

我怎么用C#运行它???

start(s); 不行啊....

执行bat有两种做法每一种是启动一个cmd.exe的进程,然后在StardardInput中输入a.bat,这种办法比较靠谱;另一种是直接把a.bat当作exe来运行,这种办法依赖于环境中对bat的注册,不靠谱。
另外尽可能使用绝对路径而不是相对于CurrentDictionary的相对路径,因为你无法确定CurrentDictionary是不是你的程序所在目录。

前者的代码如下(添加using System.Diagnostics; )

Process p=new Process();
p.StartInfo.FileName="cmd.exe";
p.Start();
p.StandardInput.WriteLine(Application.StartupPath+@"\a.bat");
p.WaiteForExit();
p.Close();

using System.Diagnostics;

string s="a.bat";
ProcessStartInfo dd = new ProcessStartInfo();
dd.FileName =Path.Combine(Application.StartupPath ,s);
Process.Start(dd);