使用C#访问共享文件夹里的文件有账号密码
可以使用System.IO命名空间下的类来访问共享文件夹里的文件,同时需要提供正确的账号密码。
示例代码:
string filePath = @"\\server\share\file.txt";
string username = "username";
string password = "password";
// 创建一个网络访问对象
NetworkCredential credential = new NetworkCredential(username, password);
CredentialCache cache = new CredentialCache();
cache.Add(new Uri(filePath), "Basic", credential);
WebClient client = new WebClient();
client.Credentials = cache;
// 读取文件内容
string content = client.DownloadString(filePath);
Console.WriteLine(content);
在上面的代码中,我们使用了WebClient类来访问共享文件夹里的文件,同时提供了正确的账号密码。其中,CredentialCache类用于缓存凭据信息,以便在多次访问时重复使用。最后,使用DownloadString方法来读取文件内容
原文地址: http://www.cveoy.top/t/topic/hpSk 著作权归作者所有。请勿转载和采集!