[英]C# process start impersonation error
我正在尝试与其他用户一起运行该过程。 当我运行普通的“ notepad.exe”时,它可以正常工作。 但是当我将文件更改为具有完整路径(C:\\\\ Program Files \\\\ Microsoft Office \\\\ Office15 \\\\ Excel.exe)或(C:\\\\ Program Files(x86)\\\\ Adobe \\\\ Acrobat的任何其他可执行文件时Reader DC \\\\ Reader \\\\ AcroRd32.exe)无效。 而是给出类似图片中的错误。
有什么建议么...??
static void Main(string[] args)
{
SecureString securePwd = new SecureString();
string password = "P@ssw0rd";
SecureString sec_pass = new SecureString();
Array.ForEach(password.ToArray(), sec_pass.AppendChar);
sec_pass.MakeReadOnly();
Process p = new Process();
ProcessStartInfo ps = new ProcessStartInfo();
p.StartInfo.FileName = "notepad.exe";
p.StartInfo.Arguments = "C:\\Program Files (x86)\\Adobe\\Acrobat Reader DC\\Reader\\welcome.pdf";
p.StartInfo.WorkingDirectory = "C:\\Program Files (x86)\\Adobe\\Acrobat Reader DC\\Reader\\";
p.StartInfo.ErrorDialog = true;
p.StartInfo.EnvironmentVariables.Add("TempPath", "C:\\Temp");
p.StartInfo.Domain = "testdom";
p.StartInfo.UserName = "testuser";
p.StartInfo.Password = sec_pass;
p.StartInfo.RedirectStandardError = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
StreamReader myStreamReader = p.StandardOutput;
// Read the standard error of net.exe and write it on to console.
Console.WriteLine(myStreamReader.ReadLine());
p.Close();
}
记事本不存储任何用户特定的设置。 我确定所有Office产品都可以使用,如果Acrobat也这样做也不会令我感到惊讶。
因此,要解决的第一件事是确保ProcessStartInfo
将LoadUserProfile
设置为true
。 这可能就足够了。
但是,有时应用程序在首次运行时的行为与后续启动时的行为会有很大不同,因此,我还要确保您至少在一次运行时以预期的目标用户身份启动了这些应用程序中的每一个以该用户身份登录到计算机(与以该用户身份启动一个进程相反)。
在您的代码示例中,您尝试在记事本中打开pdf文档。
仅检查一下,将文件名更改为adobe exe(可能需要将路径添加到exe)而不是notepad.exe时会发生什么情况
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.