使用C#访问共享文件夹里的文件
使用C#访问共享文件夹里的文件可以使用System.IO命名空间下的File类和Directory类。以下是一个示例代码:
using System.IO;
string sharedFolderPath = @"\\sharedComputer\sharedFolder";
string fileName = "example.txt";
// 检查共享文件夹是否存在
if (Directory.Exists(sharedFolderPath))
{
// 拼接文件路径
string filePath = Path.Combine(sharedFolderPath, fileName);
// 检查文件是否存在
if (File.Exists(filePath))
{
// 读取文件内容
string fileContent = File.ReadAllText(filePath);
Console.WriteLine("文件内容:" + fileContent);
}
else
{
Console.WriteLine("文件不存在。");
}
}
else
{
Console.WriteLine("共享文件夹不存在。");
}
在上面的示例代码中,我们首先指定了共享文件夹的路径和文件名。然后使用Directory.Exists方法检查共享文件夹是否存在,使用File.Exists方法检查文件是否存在。如果文件存在,我们可以使用File.ReadAllText方法读取文件内容。如果文件不存在或共享文件夹不存在,我们会输出相应的提示信息
原文地址: http://www.cveoy.top/t/topic/hpSA 著作权归作者所有。请勿转载和采集!