├── RunAs ├── App.config ├── Program.cs ├── Properties │ └── AssemblyInfo.cs └── RunAs.csproj ├── README.md └── RunAs.sln /RunAs/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RunAs 2 | 3 | Build in Visual Studio (.net 3.5 so it runs on Win 7). 4 | 5 | This is a proof of concept, example project that shows a pattern for how to programmatically invoke UAC to escalate privileges of the current process to administrator in C#. 6 | -------------------------------------------------------------------------------- /RunAs.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27703.2035 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RunAs", "RunAs\RunAs.csproj", "{33A6DC26-7D7D-4117-8AF3-8D614D7D457B}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {33A6DC26-7D7D-4117-8AF3-8D614D7D457B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {33A6DC26-7D7D-4117-8AF3-8D614D7D457B}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {33A6DC26-7D7D-4117-8AF3-8D614D7D457B}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {33A6DC26-7D7D-4117-8AF3-8D614D7D457B}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {956CF13A-368C-44E8-A52F-9B581F2BF920} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /RunAs/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Security.Principal; 4 | using System.Security; 5 | using System.Net; 6 | 7 | namespace RunAs 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | RunThisAsAdmin(); 14 | Console.WriteLine("Running as admin!"); 15 | Console.ReadKey(); 16 | } 17 | 18 | private static void RunThisAsAdmin() 19 | { 20 | if (!IsAdministrator()) 21 | { 22 | var exe = Process.GetCurrentProcess().MainModule.FileName; 23 | var startInfo = new ProcessStartInfo(exe) 24 | { 25 | UseShellExecute = true, 26 | Verb = "runas", 27 | WindowStyle = ProcessWindowStyle.Normal, 28 | CreateNoWindow = false 29 | }; 30 | Process.Start(startInfo); 31 | Process.GetCurrentProcess().Kill(); 32 | } 33 | } 34 | private static bool IsAdministrator() 35 | { 36 | var identity = WindowsIdentity.GetCurrent(); 37 | var principal = new WindowsPrincipal(identity); 38 | return principal.IsInRole(WindowsBuiltInRole.Administrator); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /RunAs/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("RunAsExample")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("")] 13 | [assembly: AssemblyCopyright("Copyright © 2018")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("33a6dc26-7d7d-4117-8af3-8d614d7d457b")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.3.3.7")] 36 | [assembly: AssemblyFileVersion("1.3.3.7")] 37 | -------------------------------------------------------------------------------- /RunAs/RunAs.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {33A6DC26-7D7D-4117-8AF3-8D614D7D457B} 8 | Exe 9 | RunAs 10 | RunAsExample 11 | v4.0 12 | 512 13 | true 14 | 15 | false 16 | publish\ 17 | true 18 | Disk 19 | false 20 | Foreground 21 | 7 22 | Days 23 | false 24 | false 25 | true 26 | 7 27 | 1.3.3.%2a 28 | false 29 | true 30 | 31 | 32 | AnyCPU 33 | true 34 | full 35 | false 36 | bin\Debug\ 37 | DEBUG;TRACE 38 | prompt 39 | 4 40 | 41 | 42 | AnyCPU 43 | pdbonly 44 | true 45 | bin\Release\ 46 | TRACE 47 | prompt 48 | 4 49 | 50 | 51 | true 52 | 53 | 54 | 02F60CFE65989E6D3F905DB972BEDB7E3055D8F1 55 | 56 | 57 | RunAs_TemporaryKey.pfx 58 | 59 | 60 | true 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | False 83 | .NET Framework 3.5 SP1 84 | true 85 | 86 | 87 | 88 | --------------------------------------------------------------------------------