How can I start a process in the background?

前端 未结 2 1350
一向
一向 2020-12-14 11:36

I can\'t seem to find an answer on Google or here on StackOverflow.

How can I start a process in background (behind the active window)? Like, when the process starts

相关标签:
2条回答
  • 2020-12-14 11:45

    Try this:

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

    Also, if that doesn't work, try adding

    p.StartInfo.UseShellExecute = false;
    
    0 讨论(0)
  • 2020-12-14 12:07

    Below code should do what you need:

    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); 
    }
    
    0 讨论(0)
提交回复
热议问题