如何使控制台应用程序始终以管理员身份运行? [英] How do I make a console app always run as an administrator?

查看:458
本文介绍了如何使控制台应用程序始终以管理员身份运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制台应用程序,该应用程序经开发可被erp软件调用.

他们在erp中调用我的应用程序,当他们这样做时,我总是会收到与权限不足有关的错误.

我已经为所有用户选中了exe属性中的以管理员身份运行该程序"复选框,但结果是相同的.

我已经阅读了有关添加清单的内容,该清单将使应用程序提示打开uac对话框,但这不是我想要的,因为将从服务器上的erp调用该应用程序,而客户端将不会在服务器上看到该对话框./p>

有人可以向我解释如何使此控制台应用程序始终以管理员身份运行吗?

解决方案

首先,请了解在此过程中的某个时候会出现UAC对话框.没有办法避免它.您可以采取几种方法:

  • 让人们运行提升了的ERP软件.我仅出于完整性考虑而包括在内.通常在不需要电源时,每次运行该应用程序时都必须同意UAC提示,这很糟糕.不过,这将是一个方便的快速测试,让您确认如果您的应用程序处于提升状态,一切都会正常进行.从提升进程启动的所有内容都会提升,因此,如果您的应用仍然收到错误消息,则无法通过提升来解决.
  • 更改ERP应用程序中的代码以启动提升您的应用程序.您提到了C#.使用runas动词启动是此处的一种方法.这给ERP开发人员带来了负担,他们可能无法做到.
  • 将清单添加到您的应用.您可以嵌入一个,或者如果您的应用程序名为foo.exe,则只需使用适当的requestExecutionLevel手动创建一个foo.exe.manifest文件.要嵌入一个清单,请使用C#项目的属性"页面选择正确的清单类型.确保启动时将UseShellExecute设置为true,否则清单将被忽略.

如果选择第一个选项,则每次启动ERP应用程序时都会出现UAC提示.不好.如果选择第二个或第三个,则每次从ERP应用程序启动控制台应用程序时,都会出现UAC提示.可能可以接受.

您可能要考虑的另一件事是更加认真地研究控制台应用程序为何需要admin privs.您是在写C的根目录还是在编写程序文件?您正在更新HKLM中的注册表项吗?无论您在做什么,为什么要那样做?除非您的应用安装或配置了某些东西(在这种情况下,获得UAC提示是正确的选择),否则您应尝试对其进行调整,以便将其写入到用户前的存储中并且不需要提升.这样,无论如何您都不再担心如何启动它了.

I have a console application that was developed to be called by a erp software.

They call my app inside the erp and when they do it, i always get errors related to the insufficient permission to do it.

I have checked the "run this program as an administrator" checkbox in the properties of the exe for all users but the result is the same.

I have read something about adding a manifest that will make the app prompt for the uac dialog, but thats not what i want because the app will be called from erp on the server and clients will not see the dialog on server.

Can someone explain me how to make this console app always run as administrator?

解决方案

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.

这篇关于如何使控制台应用程序始终以管理员身份运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆