C#进程启动模拟错误

时间:2017-05-21 17:14:35

标签: c# process impersonation processstartinfo

我正在尝试使用不同的用户运行该过程。当我运行正常的“notepad.exe”时它工作正常。但是当我将文件更改为具有完整路径的任何其他可执行文件(C:\\ Program Files \\ Microsoft Office \\ Office15 \\ Excel.exe)或(C:\\ Program Files(x86)\\ Adob​​e \\ Acrobat时读卡器DC \\ Reader \\ AcroRd32.exe)它不起作用。而是给出了附加在图片中的错误。

任何建议...... ??

enter image description here enter image description here

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

        }

2 个答案:

答案 0 :(得分:1)

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

因此,首先要确保您的ProcessStartInfoLoadUserProfile设置为true可能就足够了。

然而,有时候应用程序在第一次运行时与后续启动时的行为方式完全不同,因此我还要确保您至少有一次将这些应用程序作为预期的目标用户启动,而您'实际上已将作为用户登录到计算机上(而不是仅以该用户身份启动单个进程)。

答案 1 :(得分:0)

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

只是检查,当您将文件名更改为adobe exe时会发生什么(您可能需要添加exe的路径)而不是notepad.exe