博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How do I force my .NET application to run as administrator?
阅读量:4602 次
发布时间:2019-06-09

本文共 2328 字,大约阅读时间需要 7 分钟。

You'll want to modify the manifest that gets embedded in the program. This works on Visual Studio 2008 and higher: Project + Add New Item, select "Application Manifest File". Change the <requestedExecutionLevel> element to:

The user gets the prompt when they start the program. Use wisely; their patience can wear out quickly.

 

 

First, understand that there WILL BE a UAC dialog at some point in this process. There is no way to avoid it. There are a few approaches you can take:

  • Have the people run the ERP software elevated. I am including this only for completeness. It is awful to have to consent to the UAC prompt every time you run the app when you usually don't need the powers. It would be a handy quick test, though, for you to confirm that if your app is elevated, things will work. Everything launched from an elevated process is elevated, so if your app still gets the error message, this isn't something you'll fix by elevating.
  • Change the code in the ERP app to launch your app elevated. You mention C#. Launching with the runas verb is an approach here. This puts the burden on the ERP developer and they may not be able to do it.
  • Add a manifest to your app. You can embed one, or if your app is called foo.exe just hand-create a foo.exe.manifest file with the appropriate requestedExecutionLevel. To embed one, use the Properties page of your C# project to choose the right kind of manifest. Make sure you're launched with UseShellExecute set to true, or the manifest will be ignored.

If you choose the first option, the UAC prompt will be every time the ERP app launches. Not good. If you choose the second or third, the UAC prompt will be every time your console app is launched from the ERP app. Probably acceptable.

One other thing you might consider is looking far more seriously into why the console app needs admin privs. Are you writing to the root of C or to Program Files? Are you updating a registry key in HKLM? Whatever you're doing, why are you doing it that way? Unless your app installs or configures something (in which case getting a UAC prompt is good and proper) you should try to adapt it so that it writes to pre-user storage and doesn't need elevation. Then you will no longer be worrying about how to launch it elevated, in any case.

 

 

 

 

 

 

转载于:https://www.cnblogs.com/chucklu/p/10855068.html

你可能感兴趣的文章
js中按钮控制显示隐藏以及下拉功能
查看>>
Intent
查看>>
波涛 - 证券期货投资计算机化技术分析原理(2013年3月19日)
查看>>
sqlserver存储过程中sql语句连接及datetime字段的处理
查看>>
JavaScript 测试和捕捉
查看>>
高级软件工程第二次作业——个人项目实战:数独
查看>>
Kafka主要配置
查看>>
PHP开发经验总结
查看>>
Cocos2d-x Developer Blog
查看>>
java内存泄漏
查看>>
2018春季校园招聘笔经面经合集:Java开发岗
查看>>
算法:JavaScript两数之和
查看>>
TIOBE Programming Community Index for June 2012
查看>>
GET,POST传值总结
查看>>
关于libxml2.so.2不被Gstreamer链接的解决
查看>>
Codeforces 872C Maximum splitting:数学【分解成合数之和】
查看>>
C++中的struct
查看>>
php join函数使用,很是方便
查看>>
hdu - 2266 How Many Equations Can You Find (简单dfs)
查看>>
UIView属性
查看>>