如何在后台启动流程?

时间:2015-07-25 15:00:01

标签: c# .net windows background-process

我似乎无法在Google或StackOverflow上找到答案。

如何在后台启动进程(在活动窗口后面)?比如,当进程启动时,它不会中断用户正在使用的当前应用程序。

该过程不会在当前应用程序前弹出,它只会启动。

这就是我正在使用的:

Process.Start(Chrome.exe);

Chrome在应用程序启动时会弹出。如何让它在后台启动?

我也试过了:

psi = new ProcessStartInfo ("Chrome.exe");
psi.UseShellExecute = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(psi);

但与前一个完全没有区别。

感谢。

2 个答案:

答案 0 :(得分:18)

试试这个:

 Process p = new Process();
        p.StartInfo = new ProcessStartInfo("Chrome.exe");
        p.StartInfo.WorkingDirectory = @"C:\Program Files\Chrome";
        p.StartInfo.CreateNoWindow = true;
        p.Start();

此外,如果这不起作用,请尝试添加

p.StartInfo.UseShellExecute = false;

答案 1 :(得分:4)

下面的代码应该可以满足您的需求:

class Program
{
    static void Main(string[] args)
    {
        var handle = Process.GetCurrentProcess().MainWindowHandle;
        Process.Start("Chrome.exe").WaitForInputIdle();
        SetForegroundWindow(handle.ToInt32());
        Console.ReadLine();
    }

    [DllImport("User32.dll")]
    public static extern Int32 SetForegroundWindow(int hWnd); 
}
相关问题