3

Possible Duplicate:
How to force C# App to run as administrator on Windows 7

I am building a WinForms C# appliction, and I need it to ask for administrator privileges so I can copy and open directories in C:\.

Is this possible?

The code I am going to use (if any one needs) is this:

if (!Directory.Exists("C:\\smm"))
{
    Directory.Create("gg");
}

Or something like that, but I am sure I need administrator privilege.

Anyone know how I can do this?

Community
  • 1
  • 1
user1032254
  • 441
  • 2
  • 8
  • 10
  • try this, http://stackoverflow.com/questions/2021831/c-sharp-admin-rights-for-a-single-methods – Illuminati Nov 09 '11 at 09:18
  • http://stackoverflow.com/questions/2818179/how-to-force-c-sharp-app-to-run-as-administrator-on-windows-7 as Hans Passant describes use a Manifest File – Rosmarine Popcorn Nov 09 '11 at 09:42

1 Answers1

3

You need to enable ClickOnce security settings in your project, then change the application manifest to require administrator privileges. This will cause Windows to show a UAC elevation prompt when the process starts, so the user can escalate your program to admin.

To enable ClickOnce, go into your project's properties, select the Security tab on the left, then check the "Enable ClickOnce Security Settings" box. Then go into the project's "Properties" directory, and open up the app.manifest file. In that file, there's a line that sets the required privileges:

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

You can make it require administrator privileges like this:

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

That'll make it require admin when the process starts.

Polynomial
  • 27,674
  • 12
  • 80
  • 107
  • 4
    Actually, that works by me only if ClickOnce is disabled. If not, I get the following error while building: `ClickOnce does not support the request execution level 'requireAdministrator'`. – Otiel Nov 09 '11 at 10:20
  • It seems that's the case for certain projects. Not sure what's up with that. – Polynomial Nov 09 '11 at 13:51