问题描述
我有一个需要在管理员权限运行的过程。我需要普通人运行的过程中,但我不想给大家访问...所以我创建了一个简单的类,将执行此一个任务作为管理员,使用模拟。
I have a process that needs to run under administrative privileges. I need the average joe to run the process, but I don't want to give everyone access... so I've created a simple class that will run this ONE task as an administrator, using impersonation.
代码很striaght进,但我不明白这是为什么崩溃。 HELP ?? !!
The code is VERY striaght-forward, but I can't understand why this is crashing. HELP??!!
我通过一个批处理文件运行此,我甚至已经复制需要执行到本地硬盘驱动器的文件,认为这可能是运行在网络上的应用程序权限问题。
I'm running this via a batch file, and I've even copied the file that needs to execute to the local hard drive, thinking this might be a permission issue for running an app over the network.
public static Process ImpersonateProcess(string exe, string args, string Username, string Password)
{
ProcessStartInfo psi = new ProcessStartInfo(exe);
psi.Arguments = args;
psi.UseShellExecute = false;
psi.UserName = Username;
psi.Password = new SecureString();
foreach (char c in Password.ToCharArray())
{
psi.Password.AppendChar(c);
}
Process proc = null;
Console.WriteLine("starting...");
proc = Process.Start(psi);
Console.WriteLine("started");
return proc;
}
在上面的代码中,我从来没有开始。它抛出的的Process.Start(PSI)的错误,并与一个错误信息目录名称是无效的。
In the code above, I never get to "started". It throws an error in the Process.Start(psi) and with an error message of "the directory name is invalid."
推荐答案
这可能是因为你没有设置财产。按照
文档:
It could be because you're not setting the WorkingDirectory property. According to thedocs:
重要提示:
如果提供了用户名和密码,必须设置的工作目录属性。如果没有设置该属性,默认的工作目录是%SYSTEMROOT%\system32。
The WorkingDirectory property must be set if UserName and Password are provided. If the property is not set, the default working directory is %SYSTEMROOT%\system32.
这篇关于为什么冒充的过程回报"目录名称是无效的"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!