141 lines
4.4 KiB
C#
141 lines
4.4 KiB
C#
using System.Diagnostics;
|
||
using System.IO;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using Debug = UnityEngine.Debug;
|
||
|
||
namespace Framework.Utils
|
||
{
|
||
/// <summary>
|
||
/// 文件操作工具类
|
||
/// </summary>
|
||
public static class FileUtils
|
||
{
|
||
/// <summary>
|
||
/// 异步读取文本文件
|
||
/// </summary>
|
||
/// <param name="filePath">文件路径</param>
|
||
/// <param name="encoding">编码格式,默认UTF8</param>
|
||
/// <returns>文件内容</returns>
|
||
public static async Task<string> ReadTextAsync(string filePath, Encoding encoding = null)
|
||
{
|
||
try
|
||
{
|
||
if (!File.Exists(filePath))
|
||
{
|
||
Debug.LogError($"文件不存在: {filePath}");
|
||
return null;
|
||
}
|
||
|
||
encoding = encoding ?? Encoding.UTF8;
|
||
using (StreamReader reader = new StreamReader(filePath, encoding))
|
||
{
|
||
return await reader.ReadToEndAsync();
|
||
}
|
||
}
|
||
catch (System.Exception ex)
|
||
{
|
||
Debug.LogError($"读取文件失败: {filePath}\n{ex}");
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 异步写入文本文件
|
||
/// </summary>
|
||
/// <param name="filePath">文件路径</param>
|
||
/// <param name="content">文件内容</param>
|
||
/// <param name="encoding">编码格式,默认UTF8</param>
|
||
/// <param name="append">是否追加模式</param>
|
||
/// <returns>是否写入成功</returns>
|
||
public static async Task<bool> WriteTextAsync(string filePath, string content, Encoding encoding = null, bool append = false)
|
||
{
|
||
try
|
||
{
|
||
string directory = Path.GetDirectoryName(filePath);
|
||
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
|
||
{
|
||
Directory.CreateDirectory(directory);
|
||
}
|
||
|
||
encoding = encoding ?? Encoding.UTF8;
|
||
using (StreamWriter writer = new StreamWriter(filePath, append, encoding))
|
||
{
|
||
await writer.WriteAsync(content);
|
||
}
|
||
|
||
return true;
|
||
}
|
||
catch (System.Exception ex)
|
||
{
|
||
Debug.LogError($"写入文件失败: {filePath}\n{ex}");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 检查文件是否存在
|
||
/// </summary>
|
||
/// <param name="filePath">文件路径</param>
|
||
/// <returns>是否存在</returns>
|
||
public static bool Exists(string filePath)
|
||
{
|
||
return File.Exists(filePath);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除文件
|
||
/// </summary>
|
||
/// <param name="filePath">文件路径</param>
|
||
/// <returns>是否删除成功</returns>
|
||
public static bool Delete(string filePath)
|
||
{
|
||
try
|
||
{
|
||
if (File.Exists(filePath))
|
||
{
|
||
File.Delete(filePath);
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
catch (System.Exception ex)
|
||
{
|
||
Debug.LogError($"删除文件失败: {filePath}\n{ex}");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 使用系统默认程序打开文件
|
||
/// </summary>
|
||
/// <param name="filePath">文件完整路径</param>
|
||
/// <returns>是否成功打开</returns>
|
||
public static bool OpenFile(string filePath)
|
||
{
|
||
try
|
||
{
|
||
if (!File.Exists(filePath))
|
||
{
|
||
Debug.LogError($"文件不存在: {filePath}");
|
||
return false;
|
||
}
|
||
|
||
// 使用ProcessStartInfo来指定正确的打开方式
|
||
ProcessStartInfo startInfo = new ProcessStartInfo();
|
||
startInfo.FileName = filePath;
|
||
startInfo.UseShellExecute = true; // 使用系统shell来执行,这样会用默认程序打开文件
|
||
Process.Start(startInfo);
|
||
|
||
return true;
|
||
}
|
||
catch (System.Exception ex)
|
||
{
|
||
Debug.LogError($"打开文件失败: {filePath}\n{ex}");
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
} |