Net5 访问共享文件夹 - 使用 UNCPath 类
使用 Net5 访问共享文件夹 - 使用 UNCPath 类
要在 Net5 中访问共享文件夹,您可以使用 UNCPath 类,并结合网络凭据进行身份验证。以下代码示例展示了如何使用 UNCPath 类访问共享文件夹:
using System.IO;
string path = '\\server\share\folder\file.txt';
string username = 'username';
string password = 'password';
// 创建 NetworkCredential 实例
NetworkCredential networkCredential = new NetworkCredential(username, password);
// 创建 UNCPath 实例,包含路径和网络凭据
UNCPath uncPath = new UNCPath(path, networkCredential);
// 使用 UNCPath 访问共享文件
using (FileStream fileStream = new FileStream(uncPath.Path, FileMode.Open))
{
// 读取或写入文件
}
在这个示例中,UNCPath 类是一个自定义类,它将 UNC 路径和网络凭据组合在一起,以方便访问共享文件夹。您需要将 'path' 变量替换为要访问的共享文件夹的路径,以及 'username' 和 'password' 变量,以使用正确的凭据进行身份验证。
原文地址: https://www.cveoy.top/t/topic/oUju 著作权归作者所有。请勿转载和采集!