using System;
|
using System.Collections.Generic;
|
using System.IO;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace IStation.DAL.LocalFile
|
{
|
public class FileSrcHelper<T> where T : class, new()
|
{
|
|
#region Query
|
|
/// <summary>
|
/// 获取全部
|
/// </summary>
|
public static List<T> GetAll(string path)
|
{
|
if (string.IsNullOrEmpty(path))
|
return default;
|
var fileName = FileHelper.GetFilePath<T>(path);
|
var list = FileHelper.GetFileObject<List<T>>(fileName);
|
return list;
|
}
|
|
#endregion
|
|
#region Append
|
/// <summary>
|
/// 附加一条
|
/// </summary>
|
public static bool Append(string path, T t)
|
{
|
if (string.IsNullOrEmpty(path))
|
return false;
|
if (t == null)
|
return false;
|
var fileName = FileHelper.GetFilePath<T>(path);
|
var list = FileHelper.GetFileObject<List<T>>(fileName);
|
if (list == null)
|
list = new List<T>();
|
list.Add(t);
|
FileHelper.SaveObjectFile(fileName, list);
|
return true;
|
}
|
|
/// <summary>
|
/// 附加多条
|
/// </summary>
|
public static bool Appends(string path, IEnumerable<T> list)
|
{
|
if (string.IsNullOrEmpty(path))
|
return false;
|
if (list == null || list.Count() < 1)
|
return false;
|
var fileName = FileHelper.GetFilePath<T>(path);
|
var srcList = FileHelper.GetFileObject<List<T>>(fileName);
|
if (srcList == null)
|
srcList = new List<T>();
|
srcList.AddRange(list);
|
FileHelper.SaveObjectFile(fileName, srcList);
|
return true;
|
}
|
#endregion
|
|
#region Cover
|
/// <summary>
|
/// 用一条覆盖
|
/// </summary>
|
public static bool Cover(string path, T t)
|
{
|
if (string.IsNullOrEmpty(path))
|
return false;
|
if (t == null)
|
return false;
|
var fileName = FileHelper.GetFilePath<T>(path);
|
var list = new List<T>();
|
list.Add(t);
|
FileHelper.SaveObjectFile(fileName, list);
|
return true;
|
}
|
|
/// <summary>
|
/// 用多条覆盖
|
/// </summary>
|
public static bool Covers(string path, IEnumerable<T> list)
|
{
|
if (string.IsNullOrEmpty(path))
|
return false;
|
if (list == null || list.Count() < 1)
|
return false;
|
var fileName = FileHelper.GetFilePath<T>(path);
|
FileHelper.SaveObjectFile(fileName, list);
|
return true;
|
}
|
#endregion
|
|
#region Exist
|
/// <summary>
|
/// 是否存在
|
/// </summary>
|
public static bool IsExist(string path)
|
{
|
if (string.IsNullOrEmpty(path))
|
return default;
|
var fileName = FileHelper.GetFilePath<T>(path);
|
return File.Exists(fileName);
|
|
}
|
#endregion
|
|
|
}
|
}
|