[英]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.