C# 中以管理员权限启动进程
我必须使用 System.Diagnostics.Process.Start() 启动命令行程序并以管理员身份运行它。
此操作也将由计划任务每天运行。
I have to start a command line program with System.Diagnostics.Process.Start() and run it as Administrator.
This action will also be run by a Scheduled Task every day.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我只是尝试使用:
如果我以管理员身份运行程序,则效果很好,但是当计划任务运行它时,我认为它不会考虑“runas”。
I've just try to use :
this works fine if I'm running my program as Administrator, but when the Scheduled Task runs it, it doesn't take the 'runas' in consideration I think.
使用登录名和密码运行进程的更好的安全选项是使用 SecureString 类来加密密码。以下是代码示例:
您必须在项目属性中允许不安全代码。
希望这有帮助。
A better secure option to run a process with login and password is use the SecureString class to encrypt the password. Here a sample of code:
You must allow unsafe code in your project properties.
Hope this help.
如果您使用计划任务,则可以设置要运行的任务的用户和密码。
在任务中使用管理员凭据就可以了。
使用
Process.Start
,您需要为ProcessStartInfo
提供UserName
和Password
:If you are using scheduled tasks, you can set the user and password for the task to run under.
Use the administrator credentials on the task and you will be fine.
With
Process.Start
, you need to supply theUserName
andPassword
for theProcessStartInfo
:请注意,只要有人可以仔细阅读应用程序并查看其中的内容,以明文形式在程序中存储密码就永远不会安全。使用 SecureString 将存储的密码转换为 SecureString 密码并不会使其更安全,因为明文密码仍然存在。
使用 SecureString 的最佳方法是以某种方式一次传递一个字符进行转换,而不需要在内存或硬盘驱动器上的任何位置拥有完整的未加密密码。转换该字符后,程序应该忘记它,然后继续执行下一个字符。
我认为这只能通过在用户将字符输入控制台时传递字符进行翻译来完成。
Be aware that storing a password in clear text in a program is never secure as long as someone can peruse the application and see what is in it. Using SecureString to convert a stored password into a SecureString password does not make it more secure, because the clear text password would still be present.
The best way to use SecureString is to pass a single character for conversion at a time in someway that does not require having the complete unencrypted password anywhere in memory or on the hard drive. After that character is converted, the program should forget it, and then go on to the next.
This could be done I think only by passing characters through for translation as they are being typed into the console by the user.
我发现了,我需要在常规设置中设置计划任务以最高权限运行应用程序。
I found it, I need to set the Scheduled Task to run the application with highest privileges in General settings.