How to start a process as administrator mode in C #

I have a Visual Studio Windows application project. I added code to download the installer update file. After the download is complete, the installer will require administrator rights to run. I have added a manifest file.

When a user clicks on DownloadUpdate.exe, UAC asks the user for administrator permission. Therefore, I assumed that all processes created and called in DownloadUpdate.exe will start as an administrator. Therefore, I made an installation call for my downloaded file with the following code:

Process p = new Process(); p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p.StartInfo.FileName = strFile; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; 
+60
c #
Mar 28
source share
9 answers

Try the following:

 //Vista or higher check if (System.Environment.OSVersion.Version.Major >= 6) { p.StartInfo.Verb = "runas"; } 

Alternatively, choose a manifest route for your application .

+63
Mar 28 '10 at 11:56
source share

It works when I try. I double checked two test programs:

 using System; using System.Diagnostics; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Process.Start("ConsoleApplication2.exe"); } } } 



 using System; using System.IO; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { try { File.WriteAllText(@"c:\program files\test.txt", "hello world"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.ReadLine(); } } } } 

First confirmed that I get a UAC bomb:

System.UnauthorizedAccessException: Access to path c: \ program files \ test.txt ".
// etc..

Then a manifest is added to ConsoleApplication1 with the phrase:

  <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> 

There is no bomb. And a file that I cannot easily delete :) This is consistent with several previous tests on different machines with Vista and Win7. A running program inherits a security token from the startup program. If the starter has acquired administrator privileges, the running program also has them.

+17
Mar 28 '10 at 13:33
source share

This is a clear answer to your question: How do I get a .NET application to run as an administrator?

Summary:

Right click on the project -> Add new item -> Application manifest file

Then in this file, change the line as follows:

 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> 

Compile and run!

+15
May 03 '12 at 8:54 am
source share
 var pass = new SecureString(); pass.AppendChar('s'); pass.AppendChar('e'); pass.AppendChar('c'); pass.AppendChar('r'); pass.AppendChar('e'); pass.AppendChar('t'); Process.Start("notepad", "admin", pass, ""); 

Also works with ProcessStartInfo :

 var psi = new ProcessStartInfo { FileName = "notepad", UserName = "admin", Domain = "", Password = pass, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true }; Process.Start(psi); 
+13
Mar 28
source share

First of all, you need to include in your project

 using System.Diagnostics; 

After that, you can write a generic method that you can use for the different .exe files that you want to use. It will look like this:

 public void ExecuteAsAdmin(string fileName) { Process proc = new Process(); proc.StartInfo.FileName = fileName; proc.StartInfo.UseShellExecute = true; proc.StartInfo.Verb = "runas"; proc.Start(); } 

If you want, for example, to execute notepad.exe, then all you do is call this method:

 ExecuteAsAdmin("notepad.exe"); 
+10
May 30 '16 at 6:44
source share

Here is an example of starting a process as an administrator without Windows Prompt

  Process p = new Process(); p.StartInfo.FileName = Server.MapPath("process.exe"); p.StartInfo.Arguments = ""; p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.Verb = "runas"; p.Start(); p.WaitForExit(); 
+3
Jan 08 '16 at 6:00
source share

You probably need to install the application as an x64 application.

IIS Snap In works only in 64-bit mode and does not work in 32-bit mode, and a process created from a 32-bit application seems to work as a 32-bit process, and the same applies to 64-bit applications.

Look: Run the process as 64 bit

0
May 12 '15 at 6:25
source share

Use this method:

 public static int RunProcessAsAdmin(string exeName, string parameters) { try { System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.UseShellExecute = true; startInfo.WorkingDirectory = CurrentDirectory; startInfo.FileName = Path.Combine(CurrentDirectory, exeName); startInfo.Verb = "runas"; //MLHIDE startInfo.Arguments = parameters; startInfo.ErrorDialog = true; Process process = System.Diagnostics.Process.Start(startInfo); process.WaitForExit(); return process.ExitCode; } catch (Win32Exception ex) { WriteLog(ex); switch (ex.NativeErrorCode) { case 1223: return ex.NativeErrorCode; default: return ErrorReturnInteger; } } catch (Exception ex) { WriteLog(ex); return ErrorReturnInteger; } } 

Hope this helps.

0
Jun 14 '16 at 12:16
source share
  Process proc = new Process(); ProcessStartInfo info = new ProcessStartInfo("Your Process name".exe, "Arguments"); info.WindowStyle = ProcessWindowStyle.Hidden; info.UseShellExecute =true; info.Verb ="runas"; proc.StartInfo = info; proc.Start(); 
0
Jul 29 '16 at 7:05
source share



All Articles