从win form C#访问远程文件夹

时间:2017-03-31 12:14:37

标签: c# winforms shared-directory

我正在C#上开发一个winform应用程序,我需要访问一个网络文件夹但是我得到了登录用户密码异常。如何通过应用程序提供用户凭据?

double length = ((Directory.GetFiles("\\192.168.50.107\Destination", "*", SearchOption.AllDirectories).Sum(t => (new FileInfo(t).Length)) / 1024) / 1024) / 1024;

2 个答案:

答案 0 :(得分:0)

试试这个

using (new NetworkConnection(@"\\192.168.50.107\Destination", readCredentials))
using (new NetworkConnection(@"\\192.168.50.107\Destination", writeCredentials)) {
   double length = ((Directory.GetFiles("\\192.168.50.107\Destination", "*",    SearchOption.AllDirectories).Sum(t => (new FileInfo(t).Length)) / 1024) / 1024) /   1024;
}

字体:How to provide user name and password when connecting to a network share

答案 1 :(得分:0)

 private void ConnectToNetwork()
    {
        NetworkCredential theNetworkCredential = new NetworkCredential(@"UserName", "Password");
        CredentialCache theNetCache = new CredentialCache();
        theNetCache.Add(new Uri(@"\\NAS"), "Basic", theNetworkCredential);
    }