如何启动进程,例如当用户单击按钮时启动 URL?
13 回答
正如马特汉密尔顿所建议的那样,您对过程的控制有限的快速方法是在 System.Diagnostics.Process 类上使用静态 Start 方法......
using System.Diagnostics;
...
Process.Start("process.exe");
另一种方法是使用 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.
这种方法允许的控制比我提到的要多得多。
您可以使用System.Diagnostics.Process.Start方法来启动一个进程。您甚至可以将 URL 作为字符串传递,它会启动默认浏览器。
正如马特所说,使用Process.Start。
您可以传递 URL 或文档。它们将由注册的应用程序启动。
例子:
Process.Start("Test.Txt");
这将启动 Notepad.exe 并加载 Text.Txt。
我在自己的程序中使用了以下内容。
Process.Start("http://www.google.com/etc/etc/test.txt")
这有点基本,但它对我有用。
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "/YourSubDirectory/yourprogram.exe");
Process.Start(new ProcessStartInfo(path));
class ProcessStart
{
static void Main(string[] args)
{
Process notePad = new Process();
notePad.StartInfo.FileName = "notepad.exe";
notePad.StartInfo.Arguments = "ProcessStart.cs";
notePad.Start();
}
}
使用进程类。MSDN 文档有一个如何使用它的示例。
您可以使用此语法运行任何应用程序:
System.Diagnostics.Process.Start("Example.exe");
对于 URL 也是如此。只需在这之间写下您的 URL ()
。
例子:
System.Diagnostics.Process.Start("http://www.google.com");
声明这个
[DllImport("user32")]
private static extern bool SetForegroundWindow(IntPtr hwnd);
[DllImport("user32")]
private static extern bool ShowWindowAsync(IntPtr hwnd, int a);
并将它放在你的函数中(注意“checkInstalled”是可选的,但如果你要使用它,你必须实现它)
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);
}
}
}
注意:我不确定在运行多个 .exe 实例时这是否有效。
如果在 Windows 上使用
Process process = new Process();
process.StartInfo.FileName = "Test.txt";
process.Start();
适用于 .Net Framework 但对于 Net core 3.1 还需要将 UseShellExecute 设置为 true
Process process = new Process();
process.StartInfo.FileName = "Test.txt";
process.StartInfo.UseShellExecute = true;
process.Start();
Include the using System.Diagnostics;
.
And then call this Process.Start("Paste your URL string here!");
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.
例如,要启动Microsoft Word ,请使用以下代码:
private void button1_Click(object sender, EventArgs e)
{
string ProgramName = "winword.exe";
Process.Start(ProgramName);
}
有关更多说明,请查看此链接。
您可以使用以下语法:
private void button1_Click(object sender, EventArgs e) {
System.Diagnostics.Process.Start(/*your file name goes here*/);
}
甚至这样:
using System;
using System.Diagnostics;
//rest of the code
private void button1_Click(object sender, EventArgs e) {
Process.Start(/*your file name goes here*/);
}
两种方法都将执行相同的任务。