I have a WPF project in Visual Studio 2013, this project have two buttons. The first button say Start Service and the second say Stop Service. When I run my Visual Studio as Administrator, the buttons work. But when I open my Visual Studio without privilages, the InvalidOperationException exception appear.
How to force my project start with privilages when Visual Studio doesn't run as administrator?
I added app.manifest to my project and change for
level="requireAdministrator" uiAccess="false"/>
but it didn't function.
For start or stop my service, I am using ServiceController.
As Torben M. Philippsen mentions in his article:
Inside the manifest file, change the existing configuration from
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
To
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Save and close the manifest file.
manifest file selection
Compile and run the application. If Your UAC settings are enabled, You will be prompted to allow the application to start in elevated mode.
Sometimes it can come in handy to check whether Your application is actually running in elevated mode or not. Maybe You will find this codesnippet usefull:
WindowsPrincipal myPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
if (myPrincipal.IsInRole(WindowsBuiltInRole.Administrator) == false )
{
//show messagebox - displaying a messange to the user that rights are missing
MessageBox.Show("You need to run the application using the \"run as administrator\" option", "administrator right required", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
MessageBox.Show("You are good to go - application running in elevated mode", "Good job" ,MessageBoxButtons.OK, MessageBoxIcon.Information);
}