156 lines
5.9 KiB
C#
156 lines
5.9 KiB
C#
|
using System;
|
|||
|
using System.ComponentModel;
|
|||
|
using System.IO;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
namespace ZXL.Helper
|
|||
|
{
|
|||
|
public static class FileHelper
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 不存在文件夹就创建
|
|||
|
/// </summary>
|
|||
|
/// <param name="srcRootPath"></param>
|
|||
|
public static void CreateDirectory(string srcRootPath)
|
|||
|
{
|
|||
|
if (!Directory.Exists(srcRootPath))
|
|||
|
Directory.CreateDirectory(srcRootPath);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 删除文件夹(递归删除所有子文件)
|
|||
|
/// </summary>
|
|||
|
/// <param name="srcRootPath"></param>
|
|||
|
public static void DeleteFolder(string srcRootPath)
|
|||
|
{
|
|||
|
if (!Directory.Exists(srcRootPath))
|
|||
|
return;
|
|||
|
|
|||
|
DirectoryInfo dir = new DirectoryInfo(srcRootPath);
|
|||
|
dir.Delete(true);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 移动文件夹到指定位置(包括所有子文件)
|
|||
|
/// </summary>
|
|||
|
/// <param name="srcRootPath"></param>
|
|||
|
/// <param name="destRootPath"></param>
|
|||
|
public static void MoveFolderWithFiles(string srcRootPath, string destRootPath)
|
|||
|
{
|
|||
|
CopyFolderWithFiles(srcRootPath, destRootPath);
|
|||
|
DeleteFolder(srcRootPath);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 重命名
|
|||
|
/// </summary>
|
|||
|
/// <param name="srcFilePath"></param>
|
|||
|
/// <param name="newName"></param>
|
|||
|
public static string RenameFile(string srcFilePath, string newName)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(newName))
|
|||
|
return null;
|
|||
|
|
|||
|
var fileInfo = new FileInfo(srcFilePath);
|
|||
|
var newPath = Path.Combine(fileInfo.DirectoryName, newName) + fileInfo.Extension;
|
|||
|
Debug.Log(newPath);
|
|||
|
fileInfo.MoveTo(newPath);
|
|||
|
return newPath;
|
|||
|
}
|
|||
|
|
|||
|
public static string RenameFolder(string srcFilePath, string newName)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(newName))
|
|||
|
return null;
|
|||
|
|
|||
|
var directoryInfo = new DirectoryInfo(srcFilePath);
|
|||
|
var newPath = Path.Combine(directoryInfo.Parent.FullName, newName);
|
|||
|
Debug.Log(newPath);
|
|||
|
directoryInfo.MoveTo(newPath);
|
|||
|
return newPath;
|
|||
|
}
|
|||
|
|
|||
|
public static void RenameFiles(string srcFilePath, string newName)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(newName))
|
|||
|
return;
|
|||
|
var directoryInfo = new DirectoryInfo(srcFilePath);
|
|||
|
var fileInfos = directoryInfo.GetFiles();
|
|||
|
foreach (var fileInfo in fileInfos)
|
|||
|
{
|
|||
|
fileInfo.MoveTo(Path.Combine(directoryInfo.FullName, newName) + fileInfo.Extension);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static void CopyFile(string srcFilePath)
|
|||
|
{
|
|||
|
var info = new FileInfo(srcFilePath);
|
|||
|
var cop = info.Name.Split(".")[0] + " cop";
|
|||
|
string newFilePath = Path.Combine(info.Directory.FullName, cop) + info.Extension;
|
|||
|
File.Copy(srcFilePath, newFilePath, true);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 复制文件夹到指定位置(包括所有子文件)
|
|||
|
/// </summary>
|
|||
|
/// <param name="srcRootPath"></param>
|
|||
|
/// <param name="destRootPath"></param>
|
|||
|
/// <exception cref="Win32Exception"></exception>
|
|||
|
public static void CopyFolderWithFiles(string srcRootPath, string destRootPath)
|
|||
|
{
|
|||
|
if (!Directory.Exists(srcRootPath))
|
|||
|
throw new Win32Exception($"srcRootPath : {srcRootPath} not exists!");
|
|||
|
if (!Directory.Exists(destRootPath))
|
|||
|
throw new Win32Exception($"destRootPath : {destRootPath} not exists!");
|
|||
|
if (Path.GetFullPath(srcRootPath) == Path.GetFullPath(destRootPath))
|
|||
|
return;
|
|||
|
srcRootPath = srcRootPath[^1] != Path.DirectorySeparatorChar
|
|||
|
? srcRootPath + Path.DirectorySeparatorChar
|
|||
|
: srcRootPath;
|
|||
|
destRootPath = destRootPath[^1] != Path.DirectorySeparatorChar
|
|||
|
? destRootPath + Path.DirectorySeparatorChar
|
|||
|
: destRootPath;
|
|||
|
foreach (var srcFileFullName in Directory.GetFiles(srcRootPath, "*.*", SearchOption.TopDirectoryOnly))
|
|||
|
{
|
|||
|
Task.Run(() =>
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
var srcFileInfo = new FileInfo(srcFileFullName);
|
|||
|
var srcFileNameBaseRoot = srcFileInfo.FullName.Replace(srcRootPath, string.Empty);
|
|||
|
|
|||
|
var destFileFullName = Path.Combine(destRootPath, srcFileNameBaseRoot);
|
|||
|
var destDirectoryInfo = new FileInfo(destFileFullName).Directory;
|
|||
|
if (destDirectoryInfo == null)
|
|||
|
{
|
|||
|
Debug.LogError($"destDirectoryInfo is null where path is :{destFileFullName}");
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
if (!destDirectoryInfo.Exists)
|
|||
|
{
|
|||
|
destDirectoryInfo.Create();
|
|||
|
}
|
|||
|
|
|||
|
File.Copy(srcFileFullName, destFileFullName, true);
|
|||
|
Debug.Log(
|
|||
|
$"copy file: {Path.GetFullPath(srcFileFullName)} to {Path.GetFullPath(destFileFullName)}!");
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
Debug.LogError(e);
|
|||
|
throw;
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
foreach (var srcDirectoryFullName in Directory.GetDirectories(srcRootPath, "*",
|
|||
|
SearchOption.TopDirectoryOnly))
|
|||
|
{
|
|||
|
var srcDirectoryBaseRootPath = srcDirectoryFullName.Replace(srcRootPath, String.Empty);
|
|||
|
CopyFolderWithFiles(srcDirectoryFullName, Path.Combine(destRootPath, srcDirectoryBaseRootPath));
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|