如何从c#开始一个过程?

时间:2021-03-05 22:49:02

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

如何启动流程,例如当用户单击按钮时启动URL ?

11 个解决方案

#1


175  

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...

正如Matt Hamilton所建议的,快速方法是在System.Diagnostics中使用静态Start方法。流程类…

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.

另一种选择是使用Process类的实例。这允许对进程进行更多的控制,包括调度、它将运行的窗口的类型以及等待进程完成的能力(对我来说最有用)。

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.

这种方法允许比我提到的更多的控制。

#2


22  

You can use the System.Diagnostics.Process.Start method to start a process. You can even pass a URL as a string and it'll kick off the default browser.

你可以使用System.Diagnostics.Process。启动方法以启动进程。您甚至可以将URL作为字符串传递,它将启动默认浏览器。

#3


8  

Just as Matt says, use Process.Start.

就像Matt说的,使用过程。

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

您可以传递一个URL或一个文档。它们将由注册的应用程序启动。

Example:

例子:

Process.Start("Test.Txt");

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

这将启动记事本。exe与文本。三种加载。

#4


7  

I used the following in my own program.

我在自己的程序中使用了以下内容。

Process.Start("http://www.google.com/etc/etc/test.txt")

It's a bit basic, but it does the job for me.

它有点基础,但对我来说很有用。

#5


5  

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

使用过程类。MSDN文档有一个如何使用它的示例。

#6


5  

var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "/YourSubDirectory/yourprogram.exe");Process.Start(new ProcessStartInfo(path));

#7


5  

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 ().

URL也是一样的。只需在这个()之间写入URL。

Example:

例子:

System.Diagnostics.Process.Start("http://www.google.com");

#8


3  

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)

把它放到函数中(注意“checkinhalt”是可选的,但是如果要使用它,就必须实现它)

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.

注意:当.exe的多个实例运行时,我不确定这是否有效。

#9


2  

class ProcessStart{    static void Main(string[] args)    {        Process notePad = new Process();        notePad.StartInfo.FileName   = "notepad.exe";        notePad.StartInfo.Arguments = "ProcessStart.cs";        notePad.Start();    }}

#10


2  

Include the using System.Diagnostics;.

包括使用System.Diagnostics;。

And then call this Process.Start("Paste your URL string here!");

然后调用这个过程。开始(“在这里粘贴URL字符串!”);

Try something like this:

试试这样:

using System.Web.UI;using System.Web.UI.WebControls;using System.Diagnostics;namespace btnproce{    public partial class WebForm1 : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {        }        protected void Button1_Click(object sender, EventArgs e)        {            string t ="Balotelli";            Process.Start("http://google.com/search?q=" + t);        }    }}

Please note that it is a sample ASP.NET page as an example. You should try and improvise a little bit.

请注意这是一个示例ASP。NET页面就是一个例子。你应该试着随机应变。

#11


0  

To start Microsoft Word for example, use this code:

例如,要启动Microsoft Word,请使用以下代码:

private void button1_Click(object sender, EventArgs e){    string ProgramName = "winword.exe";    Process.Start(ProgramName);}

For more explanations, check out this link.

更多的解释,请看这个链接。

#1


175  

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...

正如Matt Hamilton所建议的,快速方法是在System.Diagnostics中使用静态Start方法。流程类…

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.

另一种选择是使用Process类的实例。这允许对进程进行更多的控制,包括调度、它将运行的窗口的类型以及等待进程完成的能力(对我来说最有用)。

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.

这种方法允许比我提到的更多的控制。

#2


22  

You can use the System.Diagnostics.Process.Start method to start a process. You can even pass a URL as a string and it'll kick off the default browser.

你可以使用System.Diagnostics.Process。启动方法以启动进程。您甚至可以将URL作为字符串传递,它将启动默认浏览器。

#3


8  

Just as Matt says, use Process.Start.

就像Matt说的,使用过程。

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

您可以传递一个URL或一个文档。它们将由注册的应用程序启动。

Example:

例子:

Process.Start("Test.Txt");

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

这将启动记事本。exe与文本。三种加载。

#4


7  

I used the following in my own program.

我在自己的程序中使用了以下内容。

Process.Start("http://www.google.com/etc/etc/test.txt")

It's a bit basic, but it does the job for me.

它有点基础,但对我来说很有用。

#5


5  

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

使用过程类。MSDN文档有一个如何使用它的示例。

#6


5  

var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "/YourSubDirectory/yourprogram.exe");Process.Start(new ProcessStartInfo(path));

#7


5  

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 ().

URL也是一样的。只需在这个()之间写入URL。

Example:

例子:

System.Diagnostics.Process.Start("http://www.google.com");

#8


3  

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)

把它放到函数中(注意“checkinhalt”是可选的,但是如果要使用它,就必须实现它)

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.

注意:当.exe的多个实例运行时,我不确定这是否有效。

#9


2  

class ProcessStart{    static void Main(string[] args)    {        Process notePad = new Process();        notePad.StartInfo.FileName   = "notepad.exe";        notePad.StartInfo.Arguments = "ProcessStart.cs";        notePad.Start();    }}

#10


2  

Include the using System.Diagnostics;.

包括使用System.Diagnostics;。

And then call this Process.Start("Paste your URL string here!");

然后调用这个过程。开始(“在这里粘贴URL字符串!”);

Try something like this:

试试这样:

using System.Web.UI;using System.Web.UI.WebControls;using System.Diagnostics;namespace btnproce{    public partial class WebForm1 : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {        }        protected void Button1_Click(object sender, EventArgs e)        {            string t ="Balotelli";            Process.Start("http://google.com/search?q=" + t);        }    }}

Please note that it is a sample ASP.NET page as an example. You should try and improvise a little bit.

请注意这是一个示例ASP。NET页面就是一个例子。你应该试着随机应变。

#11


0  

To start Microsoft Word for example, use this code:

例如,要启动Microsoft Word,请使用以下代码:

private void button1_Click(object sender, EventArgs e){    string ProgramName = "winword.exe";    Process.Start(ProgramName);}

For more explanations, check out this link.

更多的解释,请看这个链接。