要将共享文件夹中的文件拷贝到本地,可以使用 System.IO 命名空间中的 File 类和 Directory 类来实现。以下是一个示例代码,演示如何拷贝共享文件夹中的文件到本地:

using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string sourceFolderPath = "\\server\share"; // 共享文件夹路径
        string destinationFolderPath = "C:\destination"; // 本地目标文件夹路径

        // 拷贝共享文件夹中的文件到本地
        CopyDirectory(sourceFolderPath, destinationFolderPath);
    }

    static void CopyDirectory(string sourceDirPath, string destinationDirPath)
    {
        // 创建目标文件夹
        Directory.CreateDirectory(destinationDirPath);

        // 获取源文件夹中的文件列表
        string[] files = Directory.GetFiles(sourceDirPath);

        // 拷贝每个文件到目标文件夹
        foreach (string file in files)
        {
            string fileName = Path.GetFileName(file);
            string destinationFilePath = Path.Combine(destinationDirPath, fileName);
            File.Copy(file, destinationFilePath, true);
        }

        // 获取源文件夹中的子文件夹列表
        string[] subdirectories = Directory.GetDirectories(sourceDirPath);

        // 递归拷贝每个子文件夹到目标文件夹
        foreach (string subdirectory in subdirectories)
        {
            string subdirectoryName = Path.GetFileName(subdirectory);
            string destinationSubdirectoryPath = Path.Combine(destinationDirPath, subdirectoryName);
            CopyDirectory(subdirectory, destinationSubdirectoryPath);
        }
    }
}

在示例代码中,我们使用 CopyDirectory 方法来递归拷贝文件夹及其子文件夹的内容。该方法首先创建目标文件夹,然后获取源文件夹中的文件列表,将每个文件拷贝到目标文件夹中。接下来,它获取源文件夹中的子文件夹列表,对每个子文件夹递归调用 CopyDirectory 方法,以实现整个文件夹结构的拷贝。

请将示例代码中的 sourceFolderPath 替换为共享文件夹的实际路径,将 destinationFolderPath 替换为本地目标文件夹的路径。确保你的应用程序具有足够的权限来访问共享文件夹和目标文件夹。

C# 将共享文件夹拷贝到本地 - 代码示例和步骤

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

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