代码之家  ›  专栏  ›  技术社区  ›  MrProgram

使用参数运行exe不起作用

  •  4
  • MrProgram  · 技术社区  · 11 年前
    var p = Process.Start(@"c:\PsTools\PsExec.exe", @"C:\Windows\System32\notepad.exe");
    var err = p.StandardError.ReadToEnd();
    var msg = p.StandardOutput.ReadToEnd();
    lblStatusResponse.Text = "Err: " + err + "Msg: " + msg;
    

    为什么我的代码不工作?

    我收到错误:

    System.InvalidOperationException:StandardError尚未重定向。

    但当我添加以下内容时:

    p.StartInfo.RedirectStandardError = true;
    var p = Process.Start(@"c:\PsTools\PsExec.exe", @"C:\Windows\System32\notepad.exe");) 
    

    它仍然会得到相同的错误。

    主要问题是我想用参数执行一个exe,但我无法让它运行。

    4 回复  |  直到 11 年前
        1
  •  11
  •   Thorsten Dittmar    11 年前

    以下代码生成一个新的 p ,这将忽略您在上一个实例中更改的设置:

    var p = Process.Start(@"c:\PsTools\PsExec.exe", @"C:\Windows\System32\notepad.exe");) 
    

    因此,是否初始化并不重要 p 这样地

    p.StartInfo.RedirectStandardError = true;
    

    或者不。

    你需要做什么

    您需要创建 ProcessStartInfo 对象,配置它,然后将其传递给 Process.Start .

    ProcessStartInfo p = new ProcessStartInfo(@"c:\PsTools\PsExec.exe", @"C:\Windows\System32\notepad.exe");
    p.UseShellExecute = false;
    p.RedirectStandardError = true;
    p.RedirectStandardOutput = true;
    
    Process proc = Process.Start(p);
    
    var err = proc.StandardError.ReadToEnd();
    var msg = proc.StandardOutput.ReadToEnd();
    
        2
  •  1
  •   Adrian Salazar    11 年前

    摘自MSDN: https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput(v=vs.110).aspx

    尚未为重定向定义StandardOutput流;确保ProcessStartInfo.RedirectStandardOutput设置为true,ProcessStartInfo.UseShellExecute设置为false。

    因此,请记住按照MS的指示设置这些标志。

        3
  •  0
  •   osama ellahi    4 年前
         Process proc = new Process();
         proc.StartInfo.FileName = "cmd.exe";
         proc.StartInfo.UseShellExecute = false;
         proc.StartInfo.RedirectStandardError = true;
         proc.StartInfo.RedirectStandardOutput = true;
         proc.StartInfo.Arguments = "/C " + command; //Enter your own command
         proc.Start();
         string output =proc.StandardOutput.ReadToEnd();
    
        4
  •  -1
  •   Salem    6 年前

    我知道这不是你拥有的代码,但这是我仅有的工作代码,它将在C#中运行外部命令/进程,并将所有输出/错误返回到应用程序主窗口

    public void Processing()
    {
        //Create and start the ffmpeg process
        System.Diagnostics.ProcessStartInfo psi = new ProcessStartInfo("ffmpeg")
        { // this is fully command argument you can make it according to user input 
            Arguments = "-y -i  '/mnt/Disk2/Video/Antina03.jpg' pp.mp4 ",
            RedirectStandardOutput = true,
            WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
            UseShellExecute = false,
            RedirectStandardError=true,
            RedirectStandardInput=true
        };
        System.Diagnostics.Process ischk;
        ischk = System.Diagnostics.Process.Start(psi);
        ischk.WaitForExit();
        ////Create a streamreader to capture the output of ischk
        System.IO.StreamReader ischkout = ischk.StandardOutput;
        ischk.WaitForExit();
        if (ischk.HasExited) // this condition very important to make asynchronous output  
        {
            string output = ischkout.ReadToEnd();
            out0 = output;
        }
    
        /// in case you got error message 
        System.IO.StreamReader iserror = ischk.StandardError;
        ischk.WaitForExit();
        if (ischk.HasExited)
        {
            string output = iserror.ReadToEnd();
            out0 = output;
        }
    
    }
    

    如果要运行此进程,只需调用函数 Processing() 顺便提一下 out0 是全局变量,因此可以使用该函数。

    credit

    我使用的是MonoDevlop“Linux上的C#开发工具”,我的输出是这样的:-

    public MainWindow() : base(Gtk.WindowType.Toplevel)
    {
        Build();
        Processing();
        textview2.Buffer.Text = out0;
    
    }