using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using System.IO;
|
|
namespace IStation.DAL.LocalFile
|
{
|
/// <summary>
|
/// 文件辅助类
|
/// </summary>
|
public class FileHelper
|
{
|
private const string _extension = ".txt";//文件扩展名
|
|
/// <summary>
|
/// 获取数据文件夹路径
|
/// </summary>
|
public static string GetDataPath()
|
{
|
var path = Path.Combine(LocalFileConfig.RootDirectory, LocalFileConfig.DataFolder);
|
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 GetProjectPath(long projectId)
|
{
|
var workPath = GetWorkPath();
|
var projectPath = Path.Combine(workPath, projectId.ToString());
|
if (!Directory.Exists(projectPath))
|
Directory.CreateDirectory(projectPath);
|
return projectPath;
|
}
|
|
/// <summary>
|
/// 获取场景文件夹路径
|
/// </summary>
|
public static string GetSceneFolder(long projectId, long sceneId)
|
{
|
var projectPath = GetProjectPath(projectId);
|
var scenePath = Path.Combine(projectPath, sceneId.ToString());
|
if (!Directory.Exists(scenePath))
|
Directory.CreateDirectory(scenePath);
|
return scenePath;
|
}
|
|
/// <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();
|
}
|
|
}
|
}
|