1

I am using this code to elevate an app. The UAC prompt shows just fine. Checking isAdministrator() states the app is not elevated...? Is this possible?

    public static bool IsAdministrator()
    {
        WindowsIdentity identity = WindowsIdentity.GetCurrent();

        if (null != identity)
        {
            WindowsPrincipal principal = new WindowsPrincipal(identity);
            return principal.IsInRole(WindowsBuiltInRole.Administrator);
        }

        return false;
    }

    public static Process RunProcess(string name, string arguments)
    {
        string path = Path.GetDirectoryName(name);

        if (String.IsNullOrEmpty(path))
        {
            path = Environment.CurrentDirectory;
        }

        ProcessStartInfo info = new ProcessStartInfo
        {
            UseShellExecute = true,
            WorkingDirectory = path,
            FileName = name,
            Arguments = arguments
        };

        if (!IsAdministrator())
        {
            info.Verb = "runas";
        }

        try
        {
            return Process.Start(info);
        }

        catch (Win32Exception ex)
        {
            //Trace.WriteLine(ex);
        }

        return null;
    }
cablehead
  • 151
  • 2
  • 10
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Feb 11 '14 at 16:17
  • possible duplicate of [Process.Start with different credentials with UAC on](http://stackoverflow.com/questions/2313553/process-start-with-different-credentials-with-uac-on) – Mgetz Feb 11 '14 at 16:17
  • The code in `IsAdministrator` works fine. When executed in a process that is elevated, it will return `true`. – David Heffernan Feb 11 '14 at 16:19
  • You should use ProcessExplorer to view the security token associate to your application – meziantou Feb 11 '14 at 16:20
  • This link states that Process Explorer cannot do that ...[yet]http://forum.sysinternals.com/look-at-threads-security-token_topic4882.html – cablehead Feb 11 '14 at 16:48
  • You don't need to look at the security token of a thread but of your process so Process Explorer is OK – meziantou Feb 11 '14 at 16:52
  • You elevated the *other* process. Not your own. – Hans Passant Feb 11 '14 at 17:20

0 Answers0