问题描述
我将有一个以管理员身份运行并在端口上侦听的服务。我的GUI程序将与需要管理员权限的项目的管理员服务通信。
I will have a service that runs as administrator and listens on a port. My GUI program will talk to the administrator service for the items that require administrator privileges.
如果服务尚未运行,我需要启动它。如何让我的GUI程序以管理员身份运行命令?我假设用户会被问他们是否想继续。
If the service is not yet running, I will need to start it. How can I get my GUI program to run a command as administrator? I assume the user will be asked if they want to continue.
我希望有一个我可以在CMD窗口中键入的东西,因为这应该非常适合我的Java程序。我在想像 run-as-admin javaw my-service.jar
其中 run-as-admin
是
I am hoping there is something I could type in a CMD window because that should fit very well into my Java program. I am thinking something like run-as-admin javaw my-service.jar
where run-as-admin
is the command that asks whether to continue or not.
推荐答案
您不能以管理员身份执行批处理文件。您需要创建该文件的快捷方式,然后在快捷方式以管理员身份执行中设置标志(如果这是真正想要执行的操作)。
You can't execute a batch file as administrator. You need to create a shortcut to that file and then set the flag in the Shortcut 'Execute as Administrator', if this is really what you want to do.
这从桌面,选择快捷方式,右键单击和属性 - >快捷方式选项卡 - >高级按钮。然后选中以管理员身份复选框。
To do this from the desktop, select the Shortcut, right click and Properties->Shortcut tab->Advanced button. Then check Execute as Administrator checkbox.
要以编程方式执行此操作,请参阅
To do this programmatically, see How to set "Run as administrator" flag on shortcut created by MSI installer
这篇关于Windows:使用CMD(或Java)从非特权级别运行特权命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!