【问题标题】:Executing an external program via c# without showing the console通过 c# 执行外部程序而不显示控制台
【发布时间】:2013-11-03 19:10:14
【问题描述】:

我正在尝试从我的 C# 控制台应用程序运行 VLC,但我不能。我知道还有其他类似的问题(例如 Launching process in C# Without Distracting Console WindowC# Run external console application and no ouptut?C#: Run external console program as hidden),我从中得出以下代码:

        Process process = new Process();
        process.StartInfo.FileName = "C:\\Users\\XXXXX\\Desktop\\VLC\\vlc.exe";
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.CreateNoWindow = true;
        //process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        process.StartInfo.Arguments = " -I dummy";

        process.Start();

但是,当我注释和取消注释 WindowStyle 行时,控制台仍然显示。 怎么了?

【问题讨论】:

  • 什么是 VLC? VLC 是否有可能实例化它自己的控制台窗口?
  • 它是videolan.org。是的,实际上可能是这样。通过 -I dummy 我避免 UI 启动。
  • 我安装了 VLC 并从命令行尝试了-I dummy。它确实启动了第二个窗口。看来您可能需要以编程方式找到该窗口并将其隐藏。
  • 嗯... agat 建议使用 FindWindow,但我无法将其隐藏:我想我无法按名称找到它。此外,如果我放弃 -I dummy,则正常的 UI 会启动;如果我寻找名字,没有运气

标签: c# console-application


【解决方案1】:

试试下面的命令行开关。它记录在here

process.StartInfo.Arguments = "-I dummy --dummy-quiet";

【讨论】:

    【解决方案2】:

    正如 here 所说,只需执行以下操作:

    using System.Runtime.InteropServices;
    
    ...
      [DllImport("user32.dll")]
      public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
    
      [DllImport("user32.dll")]
      static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    
    ...
    
         //Sometimes System.Windows.Forms.Application.ExecutablePath works for the caption depending on the system you are running under.
         IntPtr hWnd = FindWindow(null, "Your console windows caption"); //put your console window caption here
         if(hWnd != IntPtr.Zero)
         {
            //Hide the window
            ShowWindow(hWnd, 0); // 0 = SW_HIDE
         }
    
    
         if(hWnd != IntPtr.Zero)
         {
            //Show window again
            ShowWindow(hWnd, 1); //1 = SW_SHOWNORMA
         }
    

    更新:

    您还应该在进程启动后添加 WaitForInputIdle:

    process.Start();
    process.WaitForInputIdle();
    

    【讨论】:

    • 仍在显示。我在类定义中添加了 DllImport,并在 Main 方法中添加了以下代码。没运气。我只能隐藏启动应用程序,而不是启动的应用程序。
    • 是的。对,你应该寻找执行的应用程序窗口,而不是执行。
    • 是的,我试过了,我插入了 C:\\Users\\XXXXX\\Desktop\\VLC\\vlc.exe 作为要查找的标题,但它似乎不起作用。其实我什至不知道如何更改外部运行进程的标题。
    • 嗯...但是窗口标题不会是路径。你应该以另一种方式找到它。试试这样的:superuser.com/questions/378790/….
    • 它说名字是 C:\Users\XXXXX\Desktop\VLC\vlc.exe
    【解决方案3】:

    您可以简单地将项目属性中的输出类型更改为 Windows 应用程序。 只需右键单击项目>属性>应用程序

    【讨论】:

    • 这不起作用:问题在于外部启动的进程。
    • 这很奇怪,它对我来说很好,也许VLC本身有问题?尝试启动其他程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 2017-11-08
    • 2012-09-01
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多