简体   繁体   English

C#进程启动模拟错误

[英]C# process start impersonation error

I am trying to run the process with different user. 我正在尝试与其他用户一起运行该过程。 When I run normal "notepad.exe" it works fine. 当我运行普通的“ notepad.exe”时,它可以正常工作。 But when I change the file to any other executable with full path(C:\\\\Program Files\\\\Microsoft Office\\\\Office15\\\\Excel.exe) or (C:\\\\Program Files (x86)\\\\Adobe\\\\Acrobat Reader DC\\\\Reader\\\\AcroRd32.exe) it doesn't work. 但是当我将文件更改为具有完整路径(C:\\\\ Program Files \\\\ Microsoft Office \\\\ Office15 \\\\ Excel.exe)或(C:\\\\ Program Files(x86)\\\\ Adob​​e \\\\ Acrobat的任何其他可执行文件时Reader DC \\\\ Reader \\\\ AcroRd32.exe)无效。 Instead gives errors like attached in pic. 而是给出类似图片中的错误。

Any suggestions...?? 有什么建议么...??

在此处输入图片说明 在此处输入图片说明

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

        }

Notepad doesn't store any user-specific settings. 记事本不存储任何用户特定的设置。 I'm certain all of the Office products do, and it wouldn't surprise me if Acrobat does too. 我确定所有Office产品都可以使用,如果Acrobat也这样做也不会令我感到惊讶。

So, first thing to fix is to make sure that your ProcessStartInfo sets LoadUserProfile to true . 因此,要解决的第一件事是确保ProcessStartInfoLoadUserProfile设置为true That may be sufficient. 可能就足够了。

However, sometimes applications also act quite differently when run for the first time versus any subsequent launches, so I'd also make sure that you have, at least once, launched each of these applications as the intended target user, whilst you're actually logged onto the machine as that user (versus just launching a single process as that user). 但是,有时应用程序在首次运行时的行为与后续启动时的行为会有很大不同,因此,我还要确保您至少在一次运行时以预期的目标用户身份启动了这些应用程序中的每一个该用户身份登录到计算机(与以该用户身份启动一个进程相反)。

In your code example your trying to open a pdf document in notepad. 在您的代码示例中,您尝试在记事本中打开pdf文档。

Just checking, what happens when you change the file name to the adobe exe (you may need to add the path to the exe) instead of notepad.exe 仅检查一下,将文件名更改为adobe exe(可能需要将路径添加到exe)而不是notepad.exe时会发生什么情况

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM