如何强制WPF应用程序在管理员模式下运行

时间:2022-06-07 07:28:32

I have an WPF application which access windows services, task schedulers on the local machine. When I deploy this WPF application and run it without "Run as Administrator" , it fails as it is not able to access the windows services and task schedulers on the local machine. If I run it with "Run as Administrator", it works correctly.

我有一个WPF应用程序,它可以访问windows服务和本地机器上的任务调度程序。当我部署这个WPF应用程序并运行它而没有“作为管理员运行”时,它失败了,因为它无法访问本地机器上的windows服务和任务调度器。如果我以“作为管理员运行”的方式运行它,它将正常工作。

How do I make my application by default run in admin mode when it is deployed in production?

当在生产中部署时,我如何使我的应用程序默认运行在管理模式中?

1 个解决方案

#1


63  

You need to add an app.manifest. Change the requestedExecutionLevel from asInvoker to requireAdministrator. You can create a new manifest by using the add file dialog, change it to require administrator. Make sure that your project settings are set to use that manifest as well. This will allow you to simply double click the application and it will automatically prompt for elevation if it isn't already.

您需要添加一个app.manifest。将requestedExecutionLevel从asInvoker更改为requireAdministrator。您可以使用add file对话框创建一个新的清单,将其更改为需要管理员。确保您的项目设置也设置为使用该清单。这将允许您简单地双击应用程序,如果还没有的话,它将自动提示海拔高度。

See here for more documentation:

更多文档请参见这里:

http://msdn.microsoft.com/en-us/library/bb756929.aspx

http://msdn.microsoft.com/en-us/library/bb756929.aspx

EDIT: For what it's worth, the article uses VS 2005 and using mt.exe to embed the manifest. if you are using Visual studio 2008+, this is built in. Simply open the properties of your Project, and on the "Application" tab you can select the manifest.

编辑:值得注意的是,本文使用VS 2005并使用mt.exe嵌入清单。如果您正在使用Visual studio 2008+,这是内置的。只需打开项目的属性,在“应用程序”选项卡上可以选择manifest。

#1


63  

You need to add an app.manifest. Change the requestedExecutionLevel from asInvoker to requireAdministrator. You can create a new manifest by using the add file dialog, change it to require administrator. Make sure that your project settings are set to use that manifest as well. This will allow you to simply double click the application and it will automatically prompt for elevation if it isn't already.

您需要添加一个app.manifest。将requestedExecutionLevel从asInvoker更改为requireAdministrator。您可以使用add file对话框创建一个新的清单,将其更改为需要管理员。确保您的项目设置也设置为使用该清单。这将允许您简单地双击应用程序,如果还没有的话,它将自动提示海拔高度。

See here for more documentation:

更多文档请参见这里:

http://msdn.microsoft.com/en-us/library/bb756929.aspx

http://msdn.microsoft.com/en-us/library/bb756929.aspx

EDIT: For what it's worth, the article uses VS 2005 and using mt.exe to embed the manifest. if you are using Visual studio 2008+, this is built in. Simply open the properties of your Project, and on the "Application" tab you can select the manifest.

编辑:值得注意的是,本文使用VS 2005并使用mt.exe嵌入清单。如果您正在使用Visual studio 2008+,这是内置的。只需打开项目的属性,在“应用程序”选项卡上可以选择manifest。