如何隐藏另一个可执行文件的控制台窗口

时间:2010-07-01 13:28:53

标签: c#

我们的C#应用​​程序将通过执行以下操作启动控制台应用程序可执行文件:Process correctionProcess = Process.Start(exePath, rawDataFileName);

客户希望隐藏来自其应用程序的控制台窗口。我们可以这样做吗?

5 个答案:

答案 0 :(得分:7)

您可以使用Process创建ProcessStartInfo,其中CreateNoWindowUseShellExecute属性分别设置为true和false。

ProcessStartInfo startInfo = new ProcessStartInfo(exePath);
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.Arguments = rawDataFileName;

Process.Start(startInfo);

答案 1 :(得分:2)

这是我最近用来做的代码:

ProcessStartInfo位于System.Diagnotics

 ProcessStartInfo startInfo          = new ProcessStartInfo();
 startInfo.CreateNoWindow            = true;
 startInfo.UseShellExecute           = false;
 startInfo.RedirectStandardOutput    = true;
 startInfo.RedirectStandardError     = true;
 startInfo.FileName                  = "your_app.exe";
 startInfo.WindowStyle               = ProcessWindowStyle.Hidden;
 startInfo.Arguments                 = "args"

    //Launch the process.
 process = Process.Start( startInfo );

答案 2 :(得分:1)

是的,有可能。您需要做的是按以下方式设置流程:

Process p = new Process();
p.StartInfo.FileName = exePath;
p.StartInfo.Arguments = rawDataFileName;
p.StartInfo.UseShellExecute = false;
p.CreateNoWindow = true;
// p.StartInfo.RedirectStandardOutput = true;
p.Start();

答案 3 :(得分:1)

只要您知道它的名称,就可以使用win API隐藏任何控制台窗口。重新使用我的MSDN帖子......

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;


class Program
{
    [DllImport("user32.dll")]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    const int SW_HIDE = 0;
    const int SW_SHOW = 5;

    static void Main()
    {
        string caption = @"file:///";
        caption += System.Reflection.Assembly.GetExecutingAssembly().Location;
        caption = caption.Replace('\\','/');

        // replace 'caption' with the exact caption of your console window
        IntPtr hWnd = FindWindow(null, caption);
        if (hWnd != IntPtr.Zero)
        {
            #region your code
            Process myProcess = new Process();
            myProcess.StartInfo.FileName = "cmd.exe";
            myProcess.StartInfo.Arguments = " /c notepad.exe";
            myProcess.StartInfo.UseShellExecute = false;
            myProcess.StartInfo.CreateNoWindow = true;
            myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            myProcess.StartInfo.LoadUserProfile = true;
            myProcess.StartInfo.RedirectStandardError = true;
            myProcess.StartInfo.RedirectStandardInput = true;
            myProcess.StartInfo.RedirectStandardOutput = true;
            myProcess.Start();
            #endregion

            ShowWindow(hWnd, SW_HIDE); // Hide console window
            myProcess.WaitForExit();//OPTIONAL
            //ShowWindow(hWnd, SW_SHOW); // Reshow console window (OPTIONAL)
        }
    }

}

答案 4 :(得分:0)

这个怎么样:

private void StartHiddenConsoleApp( string exePath, string args )
{
    var psi = new ProcessStartInfo();

    psi.Arguments = args;
    psi.CreateNoWindow = false;
    psi.ErrorDialog = false;
    psi.FileName = exePath;
    psi.WindowStyle = ProcessWindowStyle.Hidden;

    var p = Process.Start( psi );
}