Reputation: 343
I want to create a folder with my C# app. I'm writing code, for me all is ok, but my friend gets an error
Access to the path 'myfolder' is denied.
So how can I ask for admin rights? I've searched that it can be created with manifest.. What's it? How/where can I get manifest? I'm newbie sorry. I want something like this:
Upvotes: 6
Views: 1583
Reputation: 4221
Suggestion 1 to check admin rights - this one is if you want to do something with the bool value within the code after you do your checks
This will return a bool value and allow you to do what you want with theisAdmin
using System.Security.Principal;
bool isAdmin;
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
Suggest 2 - force my .NET App to run as administrator - this is the one i would do if you always want it to run as administrator and is the one i would suggest to use.
You'll want to modify the manifest that gets embedded in the program. This works on VS2008 and higher: Project + Add New Item, select "Application Manifest File". Change the <requestedExecutionLevel>
element to:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
The user gets the UAC prompt when they start the program.
Upvotes: 7