要在Net5中访问共享文件夹,您可以使用以下代码:

using System.IO;

string path = @"\\server\share\folder\file.txt";
string username = "username";
string password = "password";

// Create a new instance of the NetworkCredential class
NetworkCredential networkCredential = new NetworkCredential(username, password);

// Create a new instance of the UNC path with the network credential
UNCPath uncPath = new UNCPath(path, networkCredential);

// Use the UNC path to access the shared file
using (FileStream fileStream = new FileStream(uncPath.Path, FileMode.Open))
{
    // Read or write to the file as needed
}

在这个例子中,UNCPath类是自定义的一个类,它会将UNC路径和网络凭据组合在一起,以便访问共享文件夹。您需要将“path”变量替换为您要访问的共享文件夹的路径,以及“username”和“password”变量,以便使用正确的凭据进行身份验证

使用net5访问共享文件夹

原文地址: https://www.cveoy.top/t/topic/hrsH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录