programmatically run cmd.exe as administrator in vista, C#

后端 未结 2 1041
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-05 11:35

I have a visual studio setup and deployment project. I\'ve added a .cmd script in it. The script would need administrator privileges to run. When user clicks on the setup.ex

相关标签:
2条回答
  • 2021-01-05 11:45

    Try executing the runas command:

    ...
    
    using System.Diagnostics;
    
    ...
    
    string UserName = "user name goes here";
    ProcessStartInfo p1 = new ProcessStartInfo();
      p1.FileName = "runas";
      p1.Arguments = String.Format("/env /u:{0} cmd", UserName);
    Process.Start(p1);
    
    ...
    

    (And I don't think you need an explicit UseShellExecute)

    0 讨论(0)
  • 2021-01-05 12:04

    Just Try this, This worked for Me.

    ...
    
    using System.Diagnostics;
    
    ...
    
    ProcessStartInfo startInfo = new ProcessStartInfo();
      startInfo.UseShellExecute = true;            
      startInfo.Verb = "runas";
      startInfo.Arguments = "/env /user:" + "Administrator" + " cmd";
    Process.Start(startInfo);
    
    ...
    

    Ashutosh

    0 讨论(0)
提交回复
热议问题