|
| 1 | +using chocolatey; |
| 2 | +using chocolatey.infrastructure.logging; |
| 3 | +using System; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Reflection; |
| 6 | +using System.Security.Principal; |
| 7 | + |
| 8 | +namespace install_chocolatey |
| 9 | +{ |
| 10 | + class Program |
| 11 | + { |
| 12 | + static void Main(string[] args) |
| 13 | + { |
| 14 | + // If not running with elevated privileges, try to acquire them |
| 15 | + var identity = WindowsIdentity.GetCurrent(); |
| 16 | + var principal = new WindowsPrincipal(identity); |
| 17 | + if (!principal.IsInRole(WindowsBuiltInRole.Administrator)) |
| 18 | + { |
| 19 | + Process p = new Process(); |
| 20 | + p.StartInfo.FileName = Assembly.GetExecutingAssembly().Location; |
| 21 | + p.StartInfo.Verb = "runas"; // elevated privileges |
| 22 | + p.StartInfo.UseShellExecute = true; |
| 23 | + try |
| 24 | + { |
| 25 | + p.Start(); |
| 26 | + return; |
| 27 | + } |
| 28 | + catch |
| 29 | + { |
| 30 | + Console.WriteLine("Error: must be run as administrator"); |
| 31 | + } |
| 32 | + } |
| 33 | + else |
| 34 | + { |
| 35 | + // Set this environment variable to a sensible default value, otherwise chocolatey.lib will use the executable location |
| 36 | + if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("ChocolateyInstall"))) |
| 37 | + Environment.SetEnvironmentVariable("ChocolateyInstall", @"C:\ProgramData\chocolatey"); |
| 38 | + |
| 39 | + var choco = Lets.GetChocolatey(); |
| 40 | + choco.SetCustomLogging(new NullLog()); |
| 41 | + |
| 42 | + choco.Set(conf => conf.CommandName = "upgrade"); |
| 43 | + choco.RunConsole(new string[] { "-y", "--force", "chocolatey" }); |
| 44 | + choco.RunConsole(new string[] { "-y", "--force", "chocolatey-gui" }); |
| 45 | + |
| 46 | + Console.Write("\n\nSetup was successful. Press any key to quit.\n"); |
| 47 | + } |
| 48 | + |
| 49 | + Console.ReadKey(); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments