I am using this Impersonator class to impersonate a domain account to access a network share like so:
using(new Impersonartor(username, domain, password))
{
//Code Here
}
Copying the file from the network share works okay:
using(new Impersonartor(username, domain, password))
{
CopyAll(uncPath, localPath)
}
However, using Process.Start to view the UNC share in Explorer throws a "Logon failure: unknown user name or bad password":
using(new Impersonartor(username, domain, password))
{
Process.Start(uncPath)
}
Suspecting that the Impersonator class is at fault, I tried manually supplying the credentials to ProcessStartInfo like so:
System.Diagnostics.ProcessStartInfo viewDir = new System.Diagnostics.ProcessStartInfo(uncPath);
viewDir.UseShellExecute = false;
viewDir.Domain = netCred.Domain;
viewDir.UserName = netCred.UserName;
viewDir.Pa开发者_如何学编程ssword = ConvertToSecureString(netCred.Password);
System.Diagnostics.Process.Start(viewDir);
Still no joy. Note that I'm sure that my netCred (NetworkCredential) is correct as I've used to make prior connections to authenticated resources.
So, how do I view a UNC path in Explorer using a network credential?
I had the same problem today and here is what worked for me:
private void OpenNetworkPath(string uncPath)
{
System.Diagnostics.Process.Start("explorer.exe", uncPath);
}
Instead of passing the uncPath to the Process.Start
, try starting "explorer" in Process.Start
and pass the uncPath as ProcessStartInfo
's Arguments
property.
System.Diagnostics.ProcessStartInfo viewDir = new System.Diagnostics.ProcessStartInfo("explorer.exe");
viewDir.UseShellExecute = false;
viewDir.Domain = netCred.Domain;
viewDir.UserName = netCred.UserName;
viewDir.Password = ConvertToSecureString(netCred.Password);
viewDir.Arguments = uncPath;
System.Diagnostics.Process.Start(viewDir);
精彩评论