C# 中以管理员权限启动进程

发布于 2024-09-30 11:13:04 字数 92 浏览 12 评论 0原文

我必须使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

残龙傲雪 2024-10-07 11:13:04

我只是尝试使用:

Process p = new Process();
p.StartInfo.Verb = "runas";

如果我以管理员身份运行程序,则效果很好,但是当计划任务运行它时,我认为它不会考虑“runas”。

I've just try to use :

Process p = new Process();
p.StartInfo.Verb = "runas";

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.

羁〃客ぐ 2024-10-07 11:13:04

使用登录名和密码运行进程的更好的安全选项是使用 SecureString 类来加密密码。以下是代码示例:

string pass = "yourpass";
string name ="login";
SecureString str;
ProcessStartInfo startInfo = new ProcessStartInfo();
char[] chArray = pass.ToCharArray();
fixed (char* chRef = chArray)
{
    str = new SecureString(chRef, chArray.Length);
}
startInfo.Password = str;
startInfo.UserName = name;
Process.Start(startInfo);

您必须在项目属性中允许不安全代码。

希望这有帮助。

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:

string pass = "yourpass";
string name ="login";
SecureString str;
ProcessStartInfo startInfo = new ProcessStartInfo();
char[] chArray = pass.ToCharArray();
fixed (char* chRef = chArray)
{
    str = new SecureString(chRef, chArray.Length);
}
startInfo.Password = str;
startInfo.UserName = name;
Process.Start(startInfo);

You must allow unsafe code in your project properties.

Hope this help.

回首观望 2024-10-07 11:13:04

如果您使用计划任务,则可以设置要运行的任务的用户和密码。

在任务中使用管理员凭据就可以了。

使用Process.Start,您需要为ProcessStartInfo提供UserNamePassword

Process p = new Process("pathto.exe");
p.StartInfo.UserName = "Administrator";
p.StartInfo.Password = "password";
p.Start();

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 the UserName and Password for the ProcessStartInfo:

Process p = new Process("pathto.exe");
p.StartInfo.UserName = "Administrator";
p.StartInfo.Password = "password";
p.Start();
那一片橙海, 2024-10-07 11:13:04

请注意,只要有人可以仔细阅读应用程序并查看其中的内容,以明文形式在程序中存储密码就永远不会安全。使用 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.

春风十里 2024-10-07 11:13:04

我发现了,我需要在常规设置中设置计划任务以最高权限运行应用程序。

I found it, I need to set the Scheduled Task to run the application with highest privileges in General settings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文