我有windows表单应用程序,需要在管理员模式下运行而不编辑app.manifest文件。得到以下代码但不知道在Windows窗体应用程序中将代码放在何处。
ProcessStartInfo proc = new ProcessStartInfo();
proc.UseShellExecute = true;
proc.WorkingDirectory = Environment.CurrentDirectory;
proc.FileName = Application.ExecutablePath;
proc.Verb = "runas";
Process.Start(proc);
Application.Exit(); // Quit itself
答案 0 :(得分:0)
将此代码放在主表单加载事件
中 private void Form1_Load(object sender, EventArgs e)
{
ProcessStartInfo proc = new ProcessStartInfo();
proc.UseShellExecute = true;
proc.WorkingDirectory = Environment.CurrentDirectory;
proc.FileName = Application.ExecutablePath;
proc.Verb = "runas";
Process.Start(proc);
Application.Exit(); // Quit itself
}