How can I tell if my process is running as Administrator?

后端 未结 5 1013

I would like to display some extra UI elements when the process is being run as Administrator as opposed to when it isn\'t, similar to how Visual Studio 2008 displays \'Admi

5条回答
  •  借酒劲吻你
    2020-11-29 08:01

    I use simple try catch statement to create a random file in "C:\Windows\" folder. If it errors out the app is running with normal privileges otherwise it is running as admin privileges.

            try
            {
                File.Create(string.Format(@"C:\Windows\{0}.txt", new Guid()), 0, FileOptions.DeleteOnClose);
                // Do as admin
            }
            catch
            {
                // Do as default
            }
    

提交回复
热议问题