C# 如何使控制台应用程序始终以管理员身份运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15393288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make a console app always run as an administrator?
提问by Marco
I have a console application that was developed to be called by a erp software.
我有一个控制台应用程序,它被开发为由 erp 软件调用。
They call my app inside the erp and when they do it, i always get errors related to the insufficient permission to do it.
他们在 erp 中调用我的应用程序,当他们这样做时,我总是收到与执行权限不足相关的错误。
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.
我已经为所有用户检查了 exe 属性中的“以管理员身份运行此程序”复选框,但结果是一样的。
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.
我已经阅读了有关添加清单的内容,该清单将使应用程序提示 uac 对话框,但这不是我想要的,因为将从服务器上的 erp 调用该应用程序,而客户端将看不到服务器上的对话框。
Can someone explain me how to make this console app always run as administrator?
有人可以解释我如何使这个控制台应用程序始终以管理员身份运行吗?
采纳答案by Kate Gregory
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:
首先,了解在此过程中的某个时刻将有一个 UAC 对话。没有办法避免它。您可以采用以下几种方法:
- 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.
- 让人们运行 ERP 软件。我包括这个只是为了完整性。当您通常不需要权限时,每次运行应用程序时都必须同意 UAC 提示,这很糟糕。不过,这将是一个方便的快速测试,您可以确认如果您的应用程序被提升,一切都会正常工作。从提升的进程启动的所有内容都被提升,因此如果您的应用程序仍然收到错误消息,这不是您可以通过提升来解决的问题。
- 更改 ERP 应用程序中的代码以启动提升的应用程序。你提到了 C#。用
runas
动词启动是这里的一种方法。这给 ERP 开发人员带来了负担,他们可能无法做到。 - 向您的应用添加清单。您可以嵌入一个,或者如果您的应用程序名为 foo.exe,则只需手动创建一个具有适当 requestsExecutionLevel 的 foo.exe.manifest 文件。要嵌入一个,请使用 C# 项目的“属性”页面来选择正确的清单类型。确保启动时
UseShellExecute
设置为 true,否则清单将被忽略。
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.
如果您选择第一个选项,则每次 ERP 应用程序启动时都会出现 UAC 提示。不好。如果您选择第二个或第三个,则每次从 ERP 应用程序启动控制台应用程序时都会出现 UAC 提示。大概可以接受。
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.
您可能会考虑的另一件事是更认真地研究为什么控制台应用程序需要管理员权限。你是写到 C 的根目录还是程序文件?您是否正在更新 HKLM 中的注册表项?不管你在做什么,你为什么要那样做?除非您的应用程序安装或配置了某些东西(在这种情况下获得 UAC 提示是好的和正确的),您应该尝试调整它,以便它写入用户前存储并且不需要提升。无论如何,您将不再担心如何将其提升为高架。
回答by AnthonyLambert
create a batch file containing something like:
创建一个包含以下内容的批处理文件:
runas /env /user:%USERDOMAIN%\%USERNAME% cmd.exe /K YourProgramName.exe
Where %USERDOMAIN% and %USERNAME% are replaced by your admin account details.
其中 %USERDOMAIN% 和 %USERNAME% 被您的管理员帐户详细信息替换。
And run that instead?
而是运行它?
回答by Alexey
Add into your project Application Manifest File (Add -> New Item -> General -> Application Manifest File) and add the below node into the app.manifest:
添加到您的项目应用程序清单文件(添加 -> 新项目 -> 常规 -> 应用程序清单文件)并将以下节点添加到 app.manifest 中:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
http://msdn.microsoft.com/en-us/library/windows/desktop/bb756929.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/bb756929.aspx