Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force my C# Winforms program run as administrator on any computer?

How to force my C# Winforms program run as administrator on any computer ? and any kind of OS ?

I need code solution (any sample code will be excellent)

Thanks in advance

like image 579
Gold Avatar asked Aug 30 '10 08:08

Gold


People also ask

How can I postpone my periods?

monophasic 21-day pills, such as Microgynon and Cilest – you take a combined pill for 21 days, followed by 7 days without pills, when you have a bleed (period). To delay your period, start a new packet of pills straight after you finish the last pill and miss out the 7-day break.

Why is my period late?

Natural causes most likely to cause amenorrhea include pregnancy, breast-feeding, and menopause. Lifestyle factors may include excessive exercise and stress. Also, having too little body fat or too much body fat may also delay or stop menstruation. Hormonal imbalances may cause amenorrhea.


2 Answers

You can embed this manifest into your application.

<?xml version="1.0" encoding="utf-8" ?>  <asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">     <assemblyIdentity version="1.0.0.0" name="MyApplication" />     <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">         <security>             <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">                 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />             </requestedPrivileges>         </security>     </trustInfo> </asmv1:assembly>   
like image 189
Alex Reitbort Avatar answered Sep 20 '22 09:09

Alex Reitbort


Here is the sample code to run your application as admin.

ProcessStartInfo proc = new ProcessStartInfo(); proc.UseShellExecute = true; proc.WorkingDirectory = Environment.CurrentDirectory; proc.FileName = Application.ExecutablePath; proc.Verb = "runas"; try {     Process.Start(proc); } catch {     // The user refused the elevation.     // Do nothing and return directly ...     return; } Application.Exit();  // Quit itself 

Set the ProcessStartInfo.Verb to “runas” will let it run as admin. Here is related FAQ

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/28f84724-af3e-4fa1-bd86-b0d1499eaefa#x_FAQAnswer91

like image 23
Sandeep Pathak Avatar answered Sep 23 '22 09:09

Sandeep Pathak