zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Unity/Assets/Model/Core/Utility/Utility.File.cs

105 lines
3.0 KiB
C#

//------------------------------------------------------------
// 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
{
public static void WriteBytes(string path, byte[] buffer)
{
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
fs.Write(buffer, 0, buffer.Length);
}
}
public static void WriteString(string path, string str)
{
FileStream fs = null;
if (!File.Exists(path))
{
fs = Create(path);
}
else
{
fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
}
using (StreamWriter sw = new StreamWriter(fs))
{
sw.Write(str);
}
fs.Close();
fs.Dispose();
}
public static FileStream Create(string path)
{
if (File.Exists(path))
{
return null;
}
return File.Create(path);
}
public static void Delete(string path)
{
if (File.Exists(path))
{
File.Delete(path);
}
}
public static DirectoryInfo CreateDirectory(string path)
{
if (Directory.Exists(path))
{
return new DirectoryInfo(path);
}
return Directory.CreateDirectory(path);
}
public static void DeleteDirectory(string path, bool recursive = false)
{
if (Directory.Exists(path))
{
Directory.Delete(path, recursive);
}
}
public static void ClearDirectory(string path)
{
foreach (FileInfo info in GetFiles(path, "*.*", SearchOption.AllDirectories))
{
Delete(info.FullName);
}
}
public static FileInfo[] GetFiles(string path, string searchPattern, SearchOption searchOption)
{
if (string.IsNullOrEmpty(path) || !Directory.Exists(path))
{
Log.Error($"path 文件夹不存在");
return null;
}
DirectoryInfo info = new DirectoryInfo(path);
return info.GetFiles(searchPattern, searchOption);
}
}
}
}