通过C#访问共享文件夹
以下是通过C#访问共享文件夹的示例代码:
using System.IO;
string sharedFolder = @"\\server\share";
string username = "username";
string password = "password";
// Create a directory object for the shared folder
DirectoryInfo directory = new DirectoryInfo(sharedFolder);
// Create a network credential object for the user
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(username, password);
// Set the directory object's credential property
directory.Credential = credentials;
// Get the list of files in the shared folder
FileInfo[] files = directory.GetFiles();
// Do something with the files
foreach (FileInfo file in files)
{
Console.WriteLine(file.Name);
}
在上面的示例中,我们首先指定共享文件夹的路径,然后创建一个DirectoryInfo对象来表示该文件夹。接下来,我们创建一个NetworkCredential对象,用于指定访问该文件夹的用户名和密码。最后,我们将NetworkCredential对象分配给DirectoryInfo对象的Credential属性,并使用GetFiles方法获取文件夹中的文件列表
原文地址: https://www.cveoy.top/t/topic/hpSb 著作权归作者所有。请勿转载和采集!