C # How to check administrator rights and privileges

I want to create a folder with my C # application. I am writing the code, everything is fine for me, but my friend gets an error

Access to path "myfolder" is denied.

So how can I ask for admin rights? I was looking that it can be generated with a manifest. What is it? How / where can I prove myself? I'm newbie sorry. I want something like this:

enter image description here

+3


source to share


1 answer


Suggestion 1 for checking admin rights is one thing if you want to do something with the bool value in your code after your checks are done.

This will return a bool value and allow you to do what you want with isAdmin

using System.Security.Principal;

bool isAdmin;
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);

      

Suggest 2 - getting my .NET application to run as an administrator is the one I would do if you always wanted it to run as an administrator and this is the one I would suggest using.



You want to change the manifest that is embedded in the program. This works on VS2008 and up: Project + Add New Item, select Application Manifest File. Change the element <requestedExecutionLevel>

to:

 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

      

The user receives a UAC prompt when starting the program.

+5


source







All Articles