Cam*_*Dev 5 c# wpf service elevated-privileges visual-studio-2013
我在Visual Studio 2013中有一个WPF项目,这个项目有两个按钮.第一个按钮表示启动服务,第二个按钮表示停止服务.当我以管理员身份运行Visual Studio时,按钮可以正常工作.但是当我打开没有特权的Visual Studio时,会出现InvalidOperationException异常.
当Visual Studio不以管理员身份运行时,如何强制我的项目从特权开始?
我将app.manifest添加到我的项目中并进行了更改
level ="requireAdministrator"uiAccess ="false"/>
但它没有起作用.
为了启动或停止我的服务,我使用的是ServiceController.
正如Torben M. Philippsen在他的文章中提到的那样:
在清单文件中,从中更改现有配置
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
Run Code Online (Sandbox Code Playgroud)
至
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Run Code Online (Sandbox Code Playgroud)保存并关闭清单文件.
清单文件选择
编译并运行应用程序.如果启用了您的UAC设置,系统将提示您允许应用程序以提升模式启动.
有时它可以派上用场检查您的应用程序是否实际上是在高架模式下运行.也许你会发现这个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);
}
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
7940 次 |
| 最近记录: |