217 lines
7.8 KiB
C#
217 lines
7.8 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
|
|
namespace ZXL.Helper
|
|
{
|
|
public static class FileHelper
|
|
{
|
|
#region File
|
|
|
|
/// <summary>
|
|
/// 创建文件,并写入内容
|
|
/// </summary>
|
|
/// <param name="srcFilePath"></param>
|
|
/// <param name="bytes"></param>
|
|
public static void CreateFile(string srcFilePath, byte[] bytes)
|
|
{
|
|
FileInfo file = new FileInfo(srcFilePath);
|
|
if (file.Directory != null) file.Directory.Create();
|
|
Stream sw;
|
|
sw = file.Create();
|
|
sw.Write(bytes, 0, bytes.Length);
|
|
sw.Close();
|
|
sw.Dispose();
|
|
}
|
|
|
|
/// <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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 弃用
|
|
/// </summary>
|
|
/// <param name="srcFilePath"></param>
|
|
/// <param name="newName"></param>
|
|
[Obsolete("弃用,不允许重名")]
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 准确说这个应该叫拷贝,因为这个是复制在同一个文件夹下
|
|
/// </summary>
|
|
/// <param name="srcFilePath"></param>
|
|
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="srcFilePath"></param>
|
|
/// <param name="newFilePath"></param>
|
|
public static void MoveFile(string srcFilePath, string newFilePath)
|
|
{
|
|
var info = new FileInfo(srcFilePath);
|
|
info.MoveTo(newFilePath);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除文件(不可逆,慎用)
|
|
/// </summary>
|
|
/// <param name="srcFilePath"></param>
|
|
public static void DeleteFile(string srcFilePath)
|
|
{
|
|
FileInfo file = new FileInfo(srcFilePath);
|
|
file.Delete();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Directory
|
|
|
|
/// <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 DeleteDirectory(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 MoveDirectoryWithFiles(string srcRootPath, string destRootPath)
|
|
{
|
|
CopyFolderWithFiles(srcRootPath, destRootPath);
|
|
DeleteDirectory(srcRootPath);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 重命名文件夹
|
|
/// </summary>
|
|
/// <param name="srcFilePath"></param>
|
|
/// <param name="newName"></param>
|
|
/// <returns></returns>
|
|
public static string RenameDirectory(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;
|
|
}
|
|
|
|
/// <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));
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |