简体   繁体   English

C#使用网络凭据在指定的网络位置打开Windows资源管理器

[英]C# Open windows explorer at a specified network location with network credentials

I have written a helper tool which performs various tasks. 我写了一个执行各种任务的助手工具。 One of the tasks that it performs is to open a Windows explorer window locally, pointing to a PC on the network. 它执行的任务之一是在本地打开Windows资源管理器窗口,指向网络上的PC。 Something like opening Windows Explorer manually and entering a network location in the path \\\\192.168.201.111\\c$. 类似于手动打开Windows资源管理器,然后在路径\\\\ 192.168.201.111 \\ c $中输入网络位置。

I have done this using the built in Process class: 我使用内置的Process类来完成此操作:

var processInfo = new ProcessStartInfo
{
    Arguments = GetFullPathCDRive(), /*This results in something like:      \\192.168.202.179\c$*/
    FileName = "explorer.exe",
    UserName = Username,
    Password = GetPasswordAsSecureString(),
    Domain = Domain,
    UseShellExecute = false,
};
Process.Start(processInfo);

This code works fine if I remove the Username and Password entry from the processinfo object (assuming i've already browsed to that network location and stored the username and password) but stops working if I add it. 如果我从processinfo对象中删除“用户名”和“密码”条目(假设我已经浏览到该网络位置并存储了用户名和密码),则此代码可以正常工作,但是如果添加了它,它将停止工作。 If I try and specify the Username and Password it throws an exception with the following error even though the Username and Password are correct: 如果我尝试指定用户名和密码,即使用户名和密码正确,也会引发带有以下错误的异常:

{"Logon failure: unknown user name or bad password"} {“登录失败:未知的用户名或错误的密码”}

Does anyone have any idea why I am getting this exception? 有谁知道我为什么要得到这个例外? Should I be attempting to achieve this differently, ie not using the built in C# Process class, perhaps using some underlying Windows Calls? 我是否应该尝试以不同的方式实现这一目标,即不使用内置的C#Process类,也许不使用某些底层Windows调用?

The GetPasswordAsSecureString function is as follows if it helps. 如果有帮助,GetPasswordAsSecureString函数如下所示。 I'm using this to pass in a password string and return me a SecureString, required by the ProcessStartInfo class: 我正在使用它来传递密码字符串并为我返回SecureStart,这是ProcessStartInfo类所必需的:

public SecureString GetPasswordAsSecureString()
{
    if (Password == null)
        throw new ArgumentNullException("password");

    var securePassword = new SecureString();

    foreach (char c in Password)
        securePassword.AppendChar(c);

    securePassword.MakeReadOnly();
    return securePassword;
}

Thanks in advance for any help or advice. 在此先感谢您的帮助或建议。

Regards 问候

I couldn't find a way of passing the username and password when opening Windows Explorer but I wanted to post my solution that I went with in the end, in case it helps somebody else. 打开Windows资源管理器时,我找不到传递用户名和密码的方法,但是我想发布最终使用的解决方案,以防其他人使用。

To solve this, I used the Credential Management library to add the credentials to the Windows Credential Manager before opening explorer. 为了解决这个问题,在打开资源管理器之前,我使用了凭据管理库将凭据添加到Windows凭据管理器中。 This has the same affect as exploring to the network path before hand and adding the credentials when prompted, asking Windows to remember them. 这与事前浏览网络路径并在出现提示时添加凭据(要求Windows记住它们)具有相同的影响。

 using CredentialManagement;

 return new Credential
 {
     Target = target,
     Type = CredentialType.DomainPassword,
     PersistanceType = PersistanceType.Enterprise,
     Username = string.Format("{0}\\{1}", NetworkManager.DOMAIN, username),
     Password = password,
 }.Save();

Thanks 谢谢

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

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