How do I start a process from C#?

后端 未结 12 2008
北荒
北荒 2020-11-22 03:38

How do I start a process, such as launching a URL when the user clicks a button?

相关标签:
12条回答
  • 2020-11-22 03:49

    Just as Matt says, use Process.Start.

    You can pass a URL, or a document. They will be started by the registered application.

    Example:

    Process.Start("Test.Txt");
    

    This will start Notepad.exe with Text.Txt loaded.

    0 讨论(0)
  • 2020-11-22 03:51

    You can use this syntax for running any application:

    System.Diagnostics.Process.Start("Example.exe");
    

    And the same one for a URL. Just write your URL between this ().

    Example:

    System.Diagnostics.Process.Start("http://www.google.com");
    
    0 讨论(0)
  • 2020-11-22 03:52

    If using on Windows

    Process process = new Process();
    process.StartInfo.Filename = "Test.txt";
    process.Start();
    

    Works for .Net Framework but for Net core 3.1 also need to set UseShellExecute to true

    Process process = new Process();
    process.StartInfo.Filename = "Test.txt";
    process.StartInfo.UseShellExecute = true;
    process.Start();
    
    0 讨论(0)
  • 2020-11-22 03:53

    As suggested by Matt Hamilton, the quick approach where you have limited control over the process, is to use the static Start method on the System.Diagnostics.Process class...

    using System.Diagnostics;
    ...
    Process.Start("process.exe");
    

    The alternative is to use an instance of the Process class. This allows much more control over the process including scheduling, the type of the window it will run in and, most usefully for me, the ability to wait for the process to finish.

    using System.Diagnostics;
    ...
    Process process = new Process();
    // Configure the process using the StartInfo properties.
    process.StartInfo.FileName = "process.exe";
    process.StartInfo.Arguments = "-n";
    process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
    process.Start();
    process.WaitForExit();// Waits here for the process to exit.
    

    This method allows far more control than I've mentioned.

    0 讨论(0)
  • 2020-11-22 03:54

    Declare this

    [DllImport("user32")]
    private static extern bool SetForegroundWindow(IntPtr hwnd);
    [DllImport("user32")]
    private static extern bool ShowWindowAsync(IntPtr hwnd, int a);
    

    And put this inside your function (note that "checkInstalled" is optional, but if you'll use it, you have to implement it)

    if (ckeckInstalled("example"))
    {
        int count = Process.GetProcessesByName("example").Count();
        if (count < 1)
            Process.Start("example.exe");
        else
        {
            var proc = Process.GetProcessesByName("example").FirstOrDefault();
            if (proc != null && proc.MainWindowHandle != IntPtr.Zero)
            {
                SetForegroundWindow(proc.MainWindowHandle);
                ShowWindowAsync(proc.MainWindowHandle, 3);
            }
        }
    }
    

    NOTE: I'm not sure if this works when more than one instance of the .exe is running.

    0 讨论(0)
  • 2020-11-22 03:59

    Use the Process class. The MSDN documentation has an example how to use it.

    0 讨论(0)
提交回复
热议问题