C#进程启动模拟报错

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();

        }

在您的代码示例中,您尝试在记事本中打开 pdf 文档。

只是检查一下,当您将文件名更改为 adobe exe(您可能需要添加 exe 的路径)而不是 notepad.exe

时会发生什么

记事本不存储任何用户特定的设置。我敢肯定所有的 Office 产品都可以,如果 Acrobat 也可以,我也不会感到惊讶。

因此,首先要解决的是确保您的 ProcessStartInfo sets LoadUserProfiletrue可能就足够了。

但是,有时应用程序在 运行 首次启动时与任何后续启动时的行为也大不相同,因此我还要确保您至少启动了一次这些应用程序,因为预期的目标用户,而您实际上 作为该用户 登录计算机(而不是仅作为该用户启动单个进程)。