无法从C#Process Class运行exe

时间:2014-09-04 06:28:16

标签: .net winforms process cmd exe

我正在尝试从C#.NET Process类执行.exe文件,但我无法这样做。我能够成功地从命令提示符运行.exe。

以下是成功命令提示输出

enter image description here

我使用下面的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);

以下是上述代码的输出。

enter image description here

2 个答案:

答案 0 :(得分:4)

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();

enter image description here

答案 1 :(得分:0)

以下是此代码的成功执行。我不知道为什么我的.exe文件的输出包含在myProcess.StandardError中而不包含在myProcess.StandardOutput

我刚给了它不同的参数" myProcess.StartInfo.Arguments =" -layout D:\ Projects \ OCR \ Invoices \ 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));