如何启动进程,例如当用户单击按钮时启动 URL?
正如 Matt Hamilton 所建议的,对进程的控制有限的快速方法是在 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 作为字符串传递,它将启动默认浏览器。
正如 Matt 所说,使用 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();
}
}
使用 Process 类。 MSDN 文档有一个如何使用它的示例。
您可以使用此语法来运行任何应用程序:
System.Diagnostics.Process.Start("Example.exe");
对于 URL 也是如此。只需在
()
之间写下您的 URL。
示例:
System.Diagnostics.Process.Start("http://www.google.com");
如果在 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();
声明这一点
[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 实例正在运行时这是否有效。
包括
using System.Diagnostics;
。
然后称之为
Process.Start("Paste your URL string here!");
尝试这样的事情:
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);
}
}
}
请注意,这是一个示例 ASP.NET 页面作为示例。你应该尝试即兴创作一点。
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*/);
}
两种方法都会执行相同的任务。
openTerminal.StartInfo.FileName = "/usr/bin/osascript"; // The 'osascript' command on macOS
// AppleScript to open Terminal and execute commands
openTerminal.StartInfo.Arguments = "-e 'tell application \"Terminal\" to do script \"echo Hello World\"'";
openTerminal.StartInfo.UseShellExecute = false;
openTerminal.Start();
you can try this to run terminal on macOS and print Hello world