无法从C#进程类运行EXE
我想从C#.NET进程类执行.exe文件,但我无法这样做。我能够成功地从命令提示符运行.exe。无法从C#进程类运行EXE
下面是低于C#代码的成功的命令提示符下输出
我使用。在下面的代码之外我没有做任何事情。
string output = string.Empty;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.WorkingDirectory = @"C:\Windows\System32";
myProcess.StartInfo.FileName = "pdftotext.exe";
myProcess.StartInfo.Arguments = " -?";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.Verb = "runas";
myProcess.Start();
using (StreamReader streamReader = myProcess.StandardOutput)
output = streamReader.ReadToEnd();
MessageBox.Show("Output = " + output);
以下是上述代码的输出。
myProcess.StartInfo.FileName = "pdftotext.exe";
不要把整个文件路径,它只是要求文件名。
编辑
见我尝试相同的代码在我结束,它正确地给我的输出在我的控制台:
Process myProcess = new Process();
string output = string.Empty;
myProcess.StartInfo.UseShellExecute = false;
// myProcess.StartInfo.WorkingDirectory = @"C:\Windows\System32";
myProcess.StartInfo.FileName = @"TRACERT.EXE";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
//myProcess.StartInfo.Verb = "runas";
myProcess.Start();
//Process myProcess = Process.Start("TRACERT.EXE");
using (StreamReader streamReader = myProcess.StandardOutput)
{
output = streamReader.ReadToEnd();
}
Console.WriteLine(output);
Console.ReadLine();
显示我Nothing(例如MessageBox.Show(输出)不显示任何内容(空)) – 2014-09-04 07:36:14
尝试刷新您的streamReader。即streamReader.Flush(); – Savaratkar 2014-09-04 08:58:17
streamReader没有刷新方法StreamWriter有刷新方法 – 2014-09-04 09:13:32
下面是这个成功执行code.I不为什么我的.exe文件的输出包含在myProcess.StandardError中,而不是myProcess.StandardOutput
我只是给它不同的参数“myProcess.StartInfo.Arguments =‘-layout d:\项目\ OCR \发票\ Sample.pdf’它工作得很好
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
string output = string.Empty;
string error = string.Empty;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "pdftotext.exe";
myProcess.StartInfo.Arguments = "-layout D:\\Projects\\OCR\\Invoices\\Sample.pdf D:\\Projects\\OCR\\Invoices\\Sample.txt";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.Verb = "runas";
myProcess.Start();
using (StreamReader streamReader = myProcess.StandardOutput)
output = streamReader.ReadToEnd();
using (StreamReader streamReader = myProcess.StandardError)
error = streamReader.ReadToEnd();
if (myProcess.ExitCode > 0)
throw new Exception(ErrorCode(myProcess.ExitCode));
是什么让你觉得exe文件是在该文件夹? (它可能在你的'PATH'中的任何地方用于命令行的工作。) – Richard 2014-09-04 06:31:28
我复制粘贴他们的pdftotext.exe(即C:\ Windows \ System32 \ pdftotext.exe) – 2014-09-04 07:32:08
我想知道你是否得到了输出,但它不是你所期望的。当你调试时,在显示的消息框的哪一行上;如果包含代码中的那个,那么执行成功。然后你可以使用像Process Explorer这样的东西来看看这个过程是如何实际开始的。 – Richard 2014-09-04 08:22:57