在同一控制台中启动进程

发布于 2024-09-18 06:18:30 字数 174 浏览 8 评论 0原文

我可以在与调用程序相同的控制台中启动进程(使用 C# Process.Start())吗?这样就不会创建新窗口,并且标准输入/输出/错误将与调用控制台应用程序相同。我尝试设置 process.StartInfo.CreateNoWindow = true; 但该进程仍然在新窗口中启动(并在完成后立即关闭)。

Can I start a process (using C# Process.Start()) in the same console as the calling program? This way no new window will be created and standard input/output/error will be the same as the calling console application. I tried setting process.StartInfo.CreateNoWindow = true; but the process still starts in a new window (and immediately closes after it finishes).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谎言月老 2024-09-25 06:18:30

除了设置 UseShellExecute = false 之外,您不需要执行任何操作,因为 Win32 CreateProcess 函数用于控制台应用程序继承其父控制台,除非您指定 CREATE_NEW_CONSOLE 标志。

我尝试了以下程序:

private static void Main()
{
    Console.WriteLine( "Hello" );

    var p = new Process();
    p.StartInfo = new ProcessStartInfo( @"c:\windows\system32\netstat.exe", "-n" ) 
        {
            UseShellExecute = false
        };

    p.Start();
    p.WaitForExit();

    Console.WriteLine( "World" );
    Console.ReadLine();
}

它给了我这个输出:

alt text

You shouldn't need to do anything other than set UseShellExecute = false, as the default behaviour for the Win32 CreateProcess function is for a console application to inherit its parent's console, unless you specify the CREATE_NEW_CONSOLE flag.

I tried the following program:

private static void Main()
{
    Console.WriteLine( "Hello" );

    var p = new Process();
    p.StartInfo = new ProcessStartInfo( @"c:\windows\system32\netstat.exe", "-n" ) 
        {
            UseShellExecute = false
        };

    p.Start();
    p.WaitForExit();

    Console.WriteLine( "World" );
    Console.ReadLine();
}

and it gave me this output:

alt text

梦与时光遇 2024-09-25 06:18:30

您可以尝试重定向此进程的输出,然后将其打印在调用进程控制台上:

public class Program
{
    static void Main()
    {
        var psi = new ProcessStartInfo
        {
            FileName = @"c:\windows\system32\netstat.exe",
            Arguments = "-n",
            RedirectStandardOutput = true,
            UseShellExecute = false
        };
        var process = Process.Start(psi);
        while (!process.HasExited)
        {
            Thread.Sleep(100);
        }

        Console.WriteLine(process.StandardOutput.ReadToEnd());
    }
}

使用 Exited 事件和等待句柄的替代方法:

static void Main()
{
    using (Process p = new Process())
    {
        p.StartInfo = new ProcessStartInfo
        {
            FileName = @"netstat.exe",
            Arguments = "-n",                                        
            RedirectStandardOutput = true,
            UseShellExecute = false                    
        };
        p.EnableRaisingEvents = true;
        using (ManualResetEvent mre = new ManualResetEvent(false))
        {
            p.Exited += (s, e) => mre.Set();
            p.Start();
            mre.WaitOne();
        }

        Console.WriteLine(p.StandardOutput.ReadToEnd());
    }           
}

You could try redirecting the output of this process and then printing it on the calling process console:

public class Program
{
    static void Main()
    {
        var psi = new ProcessStartInfo
        {
            FileName = @"c:\windows\system32\netstat.exe",
            Arguments = "-n",
            RedirectStandardOutput = true,
            UseShellExecute = false
        };
        var process = Process.Start(psi);
        while (!process.HasExited)
        {
            Thread.Sleep(100);
        }

        Console.WriteLine(process.StandardOutput.ReadToEnd());
    }
}

Alternative approach using the Exited event and a wait handle:

static void Main()
{
    using (Process p = new Process())
    {
        p.StartInfo = new ProcessStartInfo
        {
            FileName = @"netstat.exe",
            Arguments = "-n",                                        
            RedirectStandardOutput = true,
            UseShellExecute = false                    
        };
        p.EnableRaisingEvents = true;
        using (ManualResetEvent mre = new ManualResetEvent(false))
        {
            p.Exited += (s, e) => mre.Set();
            p.Start();
            mre.WaitOne();
        }

        Console.WriteLine(p.StandardOutput.ReadToEnd());
    }           
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文