2012-09-20 30 views
-1

可能重複:
Process.Start() impersonation problemC#運行控制檯應用程序不同的用戶...模擬

我試圖運行控制檯應用程序,在C#中的不同用戶,但我遇到麻煩。這裏是我的代碼部分:

public void encryptPGP(string fileName) 
{ 
    string sCommandLine = "" + 
       "--recipient \"User Name <[email protected]>\" --encrypt \"" + fileName + "\""; 

    System.Diagnostics.Process.Start("C:\\Utilities\\GnuPG\\App\\gpg.exe", sCommandLine); 
} 

...我需要有C:\工具\的GnuPG \軟件\ gpg.exe跑到某個用戶。我將如何添加?

這將用於Web應用程序。

謝謝!

回答

1

您可以使用System.Diagnostics.ProcessStartInfo類來提供用戶憑據,您希望在其中啓動該過程。請參見下面的示例代碼:

string userPwd = "secretPassword"; 
System.Security.SecureString pwd = new System.Security.SecureString(); 
foreach (char c in userPwd.ToCharArray()) 
{ 
    pwd.AppendChar(c); 
} 

System.Diagnostics.ProcessStartInfo inf = new System.Diagnostics.ProcessStartInfo(); 
inf.FileName="path to file"; 
inf.Domain = "domainname"; 
inf.UserName = "desired_user_name"; 
inf.Password = pwd; 

System.Diagnostics.Process.Start(inf); 

我個人還沒有測試這個解決方案,但它應該工作框架2.0起。

希望它可以幫助你。

相關問題