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.
我在Visual Studio 2013中有一个WPF项目,这个项目有两个按钮。第一个按钮表示启动服务,第二个按钮表示停止服务。当我以管理员身份运行Visual Studio时,按钮可以正常工作。但是当我打开没有特权的Visual Studio时,会出现InvalidOperationException异常。
How to force my project start with privilages when Visual Studio doesn't run as administrator?
当Visual Studio不以管理员身份运行时,如何强制我的项目从特权开始?
I added app.manifest to my project and change for
我将app.manifest添加到我的项目中并进行更改
level="requireAdministrator" uiAccess="false"/>
but it didn't function.
但它没有起作用。
For start or stop my service, I am using ServiceController.
为了启动或停止我的服务,我使用的是ServiceController。
2 个解决方案
#1
2
This is interesting and it seems you need to change permissions of how the project runs, Try doing the following
这很有趣,似乎您需要更改项目运行方式的权限,请尝试执行以下操作
- go to project properties > Security
- enable click-once security settings and select Full trust application
转到项目属性>安全性
启用一次性安全设置并选择完全信任应用程序
More infor in this link WPF security
此链接中的更多信息WPF安全性
#2
7
As Torben M. Philippsen mentions in his article:
正如Torben M. Philippsen在他的文章中提到的那样:
- In Visual Studio 2010 (I guess the same applies to VS2008, but I haven’t tested it) right click Your project and select “add new item”
- Add a application manifest file – the default name will be app.manifest.
-
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.
保存并关闭清单文件。
- Please note that Your manifest file won’t show up anywhere in your solution. In order to fix that, in solution explorer, click the “show all files” button.
- Important: Right click the manifest file and add it to the project – we need that in order to tell VS to use the manifest file when compiling our application.
- Right click Your project and select “properties”.
- On the application tab, the bottom section, select the manifest file:
在Visual Studio 2010中(我猜这同样适用于VS2008,但我还没有测试过它)右键单击您的项目并选择“添加新项目”
添加应用程序清单文件 - 默认名称为app.manifest。
请注意,您的清单文件不会显示在解决方案的任何位置。要解决此问题,请在解决方案资源管理器中单击“显示所有文件”按钮。
重要说明:右键单击清单文件并将其添加到项目中 - 我们需要这样才能告诉VS在编译应用程序时使用清单文件。
右键单击您的项目,然后选择“属性”。
在应用程序选项卡的底部,选择清单文件:
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.
编译并运行应用程序。如果启用了您的UAC设置,系统将提示您允许应用程序以提升模式启动。
-
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:
有时它可以派上用场检查您的应用程序是否实际上是在高架模式下运行。也许你会发现这个codesnippet有用:
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); }
#1
2
This is interesting and it seems you need to change permissions of how the project runs, Try doing the following
这很有趣,似乎您需要更改项目运行方式的权限,请尝试执行以下操作
- go to project properties > Security
- enable click-once security settings and select Full trust application
转到项目属性>安全性
启用一次性安全设置并选择完全信任应用程序
More infor in this link WPF security
此链接中的更多信息WPF安全性
#2
7
As Torben M. Philippsen mentions in his article:
正如Torben M. Philippsen在他的文章中提到的那样:
- In Visual Studio 2010 (I guess the same applies to VS2008, but I haven’t tested it) right click Your project and select “add new item”
- Add a application manifest file – the default name will be app.manifest.
-
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.
保存并关闭清单文件。
- Please note that Your manifest file won’t show up anywhere in your solution. In order to fix that, in solution explorer, click the “show all files” button.
- Important: Right click the manifest file and add it to the project – we need that in order to tell VS to use the manifest file when compiling our application.
- Right click Your project and select “properties”.
- On the application tab, the bottom section, select the manifest file:
在Visual Studio 2010中(我猜这同样适用于VS2008,但我还没有测试过它)右键单击您的项目并选择“添加新项目”
添加应用程序清单文件 - 默认名称为app.manifest。
请注意,您的清单文件不会显示在解决方案的任何位置。要解决此问题,请在解决方案资源管理器中单击“显示所有文件”按钮。
重要说明:右键单击清单文件并将其添加到项目中 - 我们需要这样才能告诉VS在编译应用程序时使用清单文件。
右键单击您的项目,然后选择“属性”。
在应用程序选项卡的底部,选择清单文件:
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.
编译并运行应用程序。如果启用了您的UAC设置,系统将提示您允许应用程序以提升模式启动。
-
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:
有时它可以派上用场检查您的应用程序是否实际上是在高架模式下运行。也许你会发现这个codesnippet有用:
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); }