0

请帮助我,我在特定位置有一个 .bat 文件,我想执行它,看看如果手动单击它会发生什么情况,问题是 .bat 文件作为弹出窗口运行只是片刻,没有完成任何过程,我的代码就好像

int exitCode;
            ProcessStartInfo processInfo;
            Process process;
            string command = @"D:\programs\PreRef\Run.bat";
            processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
            //processInfo.CreateNoWindow = true;
            processInfo.UseShellExecute = false;
            // *** Redirect the output ***
            processInfo.RedirectStandardError = true;
            processInfo.RedirectStandardOutput = true;

            process = Process.Start(processInfo);
            process.WaitForExit();

            // *** Read the streams ***
            string output = process.StandardOutput.ReadToEnd();
            string error = process.StandardError.ReadToEnd();

            exitCode = process.ExitCode;
            process.Close();

所以请帮我解决这个问题。注意这个.bat文件运行另一个.exe程序,.bat文件中的文本就像PreRef.exe "D:\programs\PreRef"

4

1 回答 1

1

你可以试试这个:

Process objProcess = new Process();
objProcess.StartInfo.UseShellExecute = false;
objProcess.StartInfo.RedirectStandardOutput = true;
objProcess.StartInfo.CreateNoWindow = true;
objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;   
//file location
objProcess.StartInfo.FileName = string.Format(@"D:\programs\PreRef\Run.bat";");
//any argument 
objProcess.StartInfo.Arguments = string.Format("");
try
{
 objProcess.Start();
}
catch
{
 throw new Exception("Error");
}
StreamReader strmReader = objProcess.StandardOutput;
string strTempRow = string.Empty;
while ((strTempRow = strmReader.ReadLine()) != null)
{
    Console.WriteLine(strTempRow);
}
if (!objProcess.HasExited)
{
   objProcess.Kill();
}
于 2013-04-15T08:52:13.777 回答