从快捷方式执行批处理文件

时间:2014-07-10 22:51:06

标签: c# windows batch-file cmd

我正在尝试从桌面上的快捷方式应用程序执行批处理文件。批处理文件位于我的C:驱动器上,这是实际的application.exe所在的位置。

问题是CMD正在从C:\ Users \ hap \ Desctop>执行批处理。而不是从可执行文件路径,所以它显然找不到批处理文件正在查找我的.exe文件。

以下是我用来执行批处理文件的内容:

System.Diagnostics.Process.Start(System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\batch_file.bat").WaitForExit();

1 个答案:

答案 0 :(得分:1)

您需要做的是创建ProcessStartInfo结构并正确设置其WorkingDirectory

您应该执行以下操作:

string workingDir = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
ProcessStartInfo info = new ProcessStartInfo()
{
    FileName = workingDir + "\\batch_file.bat",
    WorkingDirectory = workingDir // or wherever else you want it to execute from
};
Process p = new Process() { StartInfo = info };
p.Start();
p.WaitForExit();