Unity 文件夹拷贝(复制)

参考代码

/// <summary>
    /// 拷贝文件夹
    /// </summary>
    /// <param name="srcPath">需要被拷贝的文件夹路径</param>
    /// <param name="tarPath">拷贝目标路径</param>
    private void CopyFolder(string srcPath, string tarPath)
    {
    
    
        if (!Directory.Exists(srcPath))
        {
    
    
            Debug.Log("CopyFolder is finish.");
            return;
        }

        if (!Directory.Exists(tarPath))
        {
    
    
            Directory.CreateDirectory(tarPath);
        }

        //获得源文件下所有文件
        List<string> files = new List<string>(Directory.GetFiles(srcPath));
        files.ForEach(f =>
        {
    
    
            string destFile = Path.Combine(tarPath, Path.GetFileName(f));
            File.Copy(f, destFile, true); //覆盖模式
        });

        //获得源文件下所有目录文件
        List<string> folders = new List<string>(Directory.GetDirectories(srcPath));
        folders.ForEach(f =>
        {
    
    
            string destDir = Path.Combine(tarPath, Path.GetFileName(f));
            CopyFolder(f, destDir); //递归实现子文件夹拷贝
        });
    }

猜你喜欢

转载自blog.csdn.net/weixin_44737486/article/details/118109177