Skip to content

Commit

Permalink
Fixed TerrariaPatcher will now run as admin on windows vista/7.
Browse files Browse the repository at this point in the history
  • Loading branch information
bladecoding committed Oct 4, 2013
1 parent eb7c019 commit 7c99435
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions TerrariaPatcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security.Principal;

namespace TerrariaPatcher
{
Expand All @@ -24,6 +26,17 @@ private static void Main(string[] args)
{
try
{
if (Environment.OSVersion.Version.Major >= 6 && !IsAdministrator())
{
Console.WriteLine("TerrariaPatcher requires admin privileges.");

var selfProc = new Process();
selfProc.StartInfo.FileName = Assembly.GetExecutingAssembly().Location;
selfProc.StartInfo.Verb = "runas";
selfProc.Start();
return;
}

if (!File.Exists(AssemblyName + ".exe"))
{
Output(AssemblyName + ".exe not found");
Expand Down Expand Up @@ -127,6 +140,13 @@ private static void Main(string[] args)
Console.ReadLine();
}

public static bool IsAdministrator()
{
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}

private static void Output(string str)
{
if (str.Length < 1)
Expand Down

0 comments on commit 7c99435

Please sign in to comment.