当用户点击按钮时,我应该如何启动进程,例如打开一个URL?
例如,要启动Microsoft Word,请使用以下代码:
private void button1_Click(object sender, EventArgs e)
{
string ProgramName = "winword.exe";
Process.Start(ProgramName);
}
For more explanations, check out this link.
请在代码中加入 using System.Diagnostics;
。
然后调用 Process.Start("粘贴你的URL字符串!");
可以尝试像下面这样:
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);
}
}
}
Process openTerminal = new Process();
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();
using System;
using System.Runtime.InteropServices;
class Demo
{
[StructLayout(LayoutKind.Sequential)]
struct StartupInfo
{
public int cb;
public string lpReserved;
public string lpDesktop;
public int lpTitle;
public int dwX;
public int dwY;
public int dwXSize;
public int dwYSize;
public int dwXCountChars;
public int dwYCountChars;
public int dwFillAttribute;
public int dwFlags;
public int wShowWindow;
public int cbReserved2;
public byte lpReserved2;
public int hStdInput;
public int hStdOutput;
public int hStdError;
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, int lpProcessAttributes,
int lpThreadAttributes, int bInheritHandles, int dwCreationFlags, int lpEnvironment,
int lpCurrentDirectory, StartupInfo lpStartupInfo, out int lpProcessInformation);
static void Main()
{
var info = new StartupInfo();
if (!CreateProcess("C:\\Windows\\System32\\cmd.exe", "/c ping", 0, 0, 0, 0, 0, 0, info, out _))
{
Console.WriteLine(Marshal.GetLastWin32Error());
Console.ReadKey();
}
}
}
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*/);
}
这两种方法都可以执行相同的任务。