我正在C#上開發一個winform應用程序,我需要訪問一個網絡文件夾,但我得到登錄用戶密碼異常。我如何通過應用程序提供用戶憑證?從獲勝形式C訪問遠程文件夾#
double length = ((Directory.GetFiles("\\192.168.50.107\Destination", "*", SearchOption.AllDirectories).Sum(t => (new FileInfo(t).Length))/1024)/1024)/1024;
我正在C#上開發一個winform應用程序,我需要訪問一個網絡文件夾,但我得到登錄用戶密碼異常。我如何通過應用程序提供用戶憑證?從獲勝形式C訪問遠程文件夾#
double length = ((Directory.GetFiles("\\192.168.50.107\Destination", "*", SearchOption.AllDirectories).Sum(t => (new FileInfo(t).Length))/1024)/1024)/1024;
試試這個
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
那裏在這種情況下可能不需要寫入憑證 – dlatikay
@ Gabriel Bernardone我試着用你的代碼與 helpm [link](http://stackoverflow.com/questions/5433570/access-a-remote-directory-from-c-sharp ) 但它給出了錯誤..「無法找到網絡名稱」 –
private void ConnectToNetwork()
{
NetworkCredential theNetworkCredential = new NetworkCredential(@"UserName", "Password");
CredentialCache theNetCache = new CredentialCache();
theNetCache.Add(new Uri(@"\\NAS"), "Basic", theNetworkCredential);
}
在嘗試訪問網絡上的文件之前調用此方法。 –
我注意到,在爲我的項目做這個時,networkcredential並不像我使用IP地址時那樣。所以我使用了IP的名字,它對我很有用。 –
我嘗試了兩種方法..與IP和服務器名稱,但它仍然給出了錯誤的路徑未找到.. ' theNetCache.Add(新Uri(@「\\ 192.168.50.107」),「基本」,theNetworkCredential); ' –
圍棋和研究模擬。 – Equalsk
可能相關http://stackoverflow.com/questions/659013/accessing-a-shared-file-unc-from-a-remote-non-trusted-domain-with-credentials – Codor
可能的重複[如何提供用戶名和密碼連接到網絡共享時](http://stackoverflow.com/questions/295538/how-to-provide-user-name-and-password-when-connecting-to-a-network-share) – Jurjen