using System.IO;
|
|
namespace IStation.DAL
|
{
|
/// <summary>
|
/// 文件辅助类
|
/// </summary>
|
public class FileHelper
|
{
|
private const string _extension = ".txt";//文件扩展名
|
|
/// <summary>
|
/// 查询数据文件夹路径
|
/// </summary>
|
public static string GetDataPath()
|
{
|
var path = Path.Combine(LocalFileConfig.RootDirectory, "Data");
|
if (!Directory.Exists(path))
|
Directory.CreateDirectory(path);
|
return path;
|
}
|
|
/// <summary>
|
/// 查询工作文件夹路径
|
/// </summary>
|
public static string GetWorkPath()
|
{
|
var dataPath = GetDataPath();
|
var workPath = Path.Combine(dataPath, LocalFileConfig.WorkFolder);
|
if (!Directory.Exists(workPath))
|
Directory.CreateDirectory(workPath);
|
return workPath;
|
}
|
|
/// <summary>
|
/// 查询版本文件夹路径
|
/// </summary>
|
public static string GetVersionsPath()
|
{
|
var workPath = GetWorkPath();
|
var projectPath = Path.Combine(workPath, LocalFileConfig.Versions);
|
if (!Directory.Exists(projectPath))
|
Directory.CreateDirectory(projectPath);
|
return projectPath;
|
}
|
|
|
/// <summary>
|
/// 查询文件路径
|
/// </summary>
|
public static string GetFilePath<T>(string folderPath) where T : class
|
{
|
if (!Directory.Exists(folderPath))
|
Directory.CreateDirectory(folderPath);
|
var objType = typeof(T);
|
var fileName = Path.Combine(folderPath, objType.Name + _extension);
|
return fileName;
|
}
|
|
/// <summary>
|
/// 保存对象到文件中
|
/// </summary>
|
public static void SaveObjectFile<T>(string fileName, T t) where T : class
|
{
|
var str = JsonHelper.Object2FormatJson(t);
|
File.WriteAllText(fileName, str);
|
}
|
|
/// <summary>
|
/// 查询文件对象
|
/// </summary>
|
public static T GetFileObject<T>(string fileName) where T : class
|
{
|
if (!File.Exists(fileName))
|
return default;
|
var str = File.ReadAllText(fileName);
|
if (string.IsNullOrEmpty(str))
|
return default;
|
return JsonHelper.Json2Object<T>(str);
|
}
|
|
|
/// <summary>
|
/// 查询ID
|
/// </summary>
|
public static long NextId()
|
{
|
return SnowflakeIdHelper.NextId();
|
}
|
|
}
|
}
|