CTT/Unity/Assets/Model/Core/Utility/Utility.File.cs

105 lines
3.0 KiB
C#
Raw Normal View History

2021-04-08 20:09:59 +08:00
//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2020 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------
using MongoDB.Bson.Serialization.Serializers;
using System.IO;
namespace ET
{
/// <summary>
/// 实用函数集。
/// </summary>
public static partial class Utility
{
public static class FileOpation
{
2021-05-01 11:27:41 +08:00
public static void WriteBytes(string path, byte[] buffer)
2021-04-08 20:09:59 +08:00
{
2021-04-11 19:50:39 +08:00
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
2021-04-08 20:09:59 +08:00
{
fs.Write(buffer, 0, buffer.Length);
}
}
2021-05-01 11:27:41 +08:00
2021-04-08 20:09:59 +08:00
public static void WriteString(string path, string str)
{
2021-05-01 11:27:41 +08:00
FileStream fs = null;
if (!File.Exists(path))
{
fs = Create(path);
}
else
2021-04-08 20:09:59 +08:00
{
2021-05-01 11:27:41 +08:00
fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
2021-04-08 20:09:59 +08:00
}
2021-05-01 11:27:41 +08:00
using (StreamWriter sw = new StreamWriter(fs))
{
sw.Write(str);
}
fs.Close();
fs.Dispose();
2021-04-08 20:09:59 +08:00
}
2021-05-01 11:27:41 +08:00
public static FileStream Create(string path)
2021-04-08 20:09:59 +08:00
{
if (File.Exists(path))
{
2021-05-01 11:27:41 +08:00
return null;
2021-04-08 20:09:59 +08:00
}
2021-05-01 11:27:41 +08:00
return File.Create(path);
2021-04-08 20:09:59 +08:00
}
2021-05-01 11:27:41 +08:00
2021-04-08 20:09:59 +08:00
public static void Delete(string path)
{
if (File.Exists(path))
{
File.Delete(path);
}
}
2021-05-01 11:27:41 +08:00
2021-04-08 20:09:59 +08:00
public static DirectoryInfo CreateDirectory(string path)
{
if (Directory.Exists(path))
{
2021-05-01 11:27:41 +08:00
return new DirectoryInfo(path);
2021-04-08 20:09:59 +08:00
}
2021-05-01 11:27:41 +08:00
2021-04-08 20:09:59 +08:00
return Directory.CreateDirectory(path);
}
2021-05-01 11:27:41 +08:00
public static void DeleteDirectory(string path, bool recursive = false)
2021-04-08 20:09:59 +08:00
{
if (Directory.Exists(path))
{
2021-05-01 11:27:41 +08:00
Directory.Delete(path, recursive);
2021-04-08 20:09:59 +08:00
}
}
2021-05-01 11:27:41 +08:00
2021-04-08 20:09:59 +08:00
public static void ClearDirectory(string path)
{
2021-05-01 11:27:41 +08:00
foreach (FileInfo info in GetFiles(path, "*.*", SearchOption.AllDirectories))
2021-04-08 20:09:59 +08:00
{
Delete(info.FullName);
}
}
2021-05-01 11:27:41 +08:00
public static FileInfo[] GetFiles(string path, string searchPattern, SearchOption searchOption)
2021-04-08 20:09:59 +08:00
{
if (string.IsNullOrEmpty(path) || !Directory.Exists(path))
{
Log.Error($"path 文件夹不存在");
return null;
}
2021-05-01 11:27:41 +08:00
2021-04-08 20:09:59 +08:00
DirectoryInfo info = new DirectoryInfo(path);
2021-05-01 11:27:41 +08:00
return info.GetFiles(searchPattern, searchOption);
2021-04-08 20:09:59 +08:00
}
}
}
2021-05-01 11:27:41 +08:00
}