2016-08-01 85 views
-1

我有Windows窗體應用程序,需要在管理員模式下運行而不編輯app.manifest文件。得到了下面的代碼,但不知道在windows窗體應用程序中放置代碼的位置。管理員模式下的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 
     } 
+0

我需要放置上述代碼的位置? – VJL

+0

所以你想運行應用程序本身作爲管理員而不是其他應用程序運行? – Mostafiz

+0

是的,我想以管理員模式運行應用程序。我如何使用c# – VJL