unity3d学习笔记之对文件夹进行拷贝
程序员文章站
2022-06-10 11:12:13
...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System;
public class UteCopyAllFileToKHD
{
private static string formPath = Application.dataPath + @"\Arts\Scenes";//原路径
private static string targetPath= @"../../../SourceCode\Client\Project\Assets\Arts\Scenes";//目标路径 ../表示当前项目文件的父路径
private static bool isNull = false;
[MenuItem("Tools/拷贝文件夹")]
static void init()
{
Copy();
}
private static void Copy()
{
isNull = false;
if(!Directory.Exists(targetPath))
{
Log.Error("未找到文件夹");
}
CleanDirectory(targetPath);
CopyDirectory(formPath, targetPath);
if (!isNull)
{
Debug.Log("Arts\\Scenes目录文件导入成功!!");
}
}
/// <summary>
/// 拷贝文件
/// </summary>
/// <param name="srcDir">起始文件夹</param>
/// <param name="tgtDir">目标文件夹</param>
public static void CopyDirectory(string srcDir, string tgtDir)
{
DirectoryInfo source = new DirectoryInfo(srcDir);
DirectoryInfo target = new DirectoryInfo(tgtDir);
if (target.FullName.StartsWith(source.FullName, StringComparison.CurrentCultureIgnoreCase))
{
throw new Exception("父目录不能拷贝到子目录!");
}
if (!source.Exists)
{
return;
}
if (!target.Exists)
{
target.Create();
}
FileInfo[] files = source.GetFiles();
DirectoryInfo[] dirs = source.GetDirectories();
if(files.Length==0&&dirs.Length==0)
{
Log.Error("当前项目中Arts\\Scenes文件夹为空");
isNull = true;
return;
}
for (int i = 0; i < files.Length; i++)
{
File.Copy(files[i].FullName, Path.Combine(target.FullName, files[i].Name), true);
}
for (int j = 0; j < dirs.Length; j++)
{
CopyDirectory(dirs[j].FullName, Path.Combine(target.FullName, dirs[j].Name));
}
}
//删除目标文件夹下面所有文件
public static void CleanDirectory( string dir)
{
foreach (string subdir in Directory.GetDirectories(dir))
{
Directory.Delete(subdir, true);
}
foreach (string subFile in Directory.GetFiles(dir))
{
File.Delete(subFile);
}
}
}
推荐阅读