C#如何通过路径访问共享文件夹
在C#中,可以通过使用System.IO命名空间中的Directory和File类来访问共享文件夹。首先需要确保计算机已经连接到共享文件夹所在的网络,并获取共享文件夹的路径。
然后可以使用以下代码来访问共享文件夹中的文件:
string sharedFolderPath = @"\\computername\sharedfolder";
string fileName = "example.txt";
string filePath = Path.Combine(sharedFolderPath, fileName);
if (File.Exists(filePath))
{
// 文件存在,可以进行读取、写入等操作
}
这里的“computername”代表共享文件夹所在的计算机名,而“sharedfolder”则代表共享文件夹的名称。在Path.Combine方法中将这两个字符串合并起来形成完整的路径,然后使用File.Exists方法来判断文件是否存在。
如果想要访问共享文件夹中的文件夹,则可以使用Directory类的方法,例如:
string sharedFolderPath = @"\\computername\sharedfolder";
string folderName = "exampleFolder";
string folderPath = Path.Combine(sharedFolderPath, folderName);
if (Directory.Exists(folderPath))
{
// 文件夹存在,可以进行读取、写入等操作
}
这里的“exampleFolder”代表共享文件夹中的文件夹名称,使用Path.Combine方法合并路径后,可以使用Directory.Exists方法来判断文件夹是否存在
原文地址: https://www.cveoy.top/t/topic/hq4J 著作权归作者所有。请勿转载和采集!