130 lines
4.3 KiB
C#
130 lines
4.3 KiB
C#
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
|
|
namespace HxBusiness
|
|
{
|
|
public class ConfigBiz
|
|
{
|
|
private const int TryReadWriteCount = 10;
|
|
private int readcount = 0;
|
|
private int writecount = 0;
|
|
/// <summary>
|
|
/// 指定程序目录
|
|
/// </summary>
|
|
private string xmlPath;
|
|
//设置读写锁
|
|
private ReaderWriterLockSlim objlock;
|
|
public ConfigBiz()
|
|
{
|
|
xmlPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "/Config/";
|
|
objlock = new ReaderWriterLockSlim();
|
|
}
|
|
/// <summary>
|
|
/// 写json到文件
|
|
/// </summary>
|
|
/// <typeparam name="T">任意模型,根据模型匹配文件名</typeparam>
|
|
/// <param name="list">返回指定模型数据</param>
|
|
/// <returns></returns>
|
|
public bool WriteDb<T>(object parm)
|
|
{
|
|
objlock.EnterWriteLock();
|
|
try
|
|
{
|
|
string fileName = typeof(T).Name.ToString() + ".json";
|
|
//如果没有则创建文件夹
|
|
if (!Directory.Exists(xmlPath))
|
|
{
|
|
Directory.CreateDirectory(xmlPath);
|
|
}//如果没有则创建文件
|
|
if (!File.Exists(xmlPath + fileName))
|
|
{
|
|
FileStream fs1 = new FileStream(xmlPath + fileName, FileMode.Create, FileAccess.ReadWrite);
|
|
fs1.Close();
|
|
}
|
|
else
|
|
{
|
|
//保证每次文件的更新
|
|
FileInfo finfo = new FileInfo(xmlPath + fileName);
|
|
finfo.Delete();
|
|
FileStream fs1 = new FileStream(xmlPath + fileName, FileMode.Create, FileAccess.ReadWrite);
|
|
fs1.Close();
|
|
}
|
|
File.WriteAllText(xmlPath + fileName, JsonConvert.SerializeObject(parm, Formatting.Indented));
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("write error!" + ex.ToString());
|
|
//CreateLog.Log.Error("write error!" + ex.ToString());
|
|
if (writecount < TryReadWriteCount)
|
|
{
|
|
Thread.Sleep(1);
|
|
writecount++;
|
|
WriteDb<T>(parm);
|
|
}
|
|
return false;
|
|
}
|
|
finally
|
|
{
|
|
objlock.ExitWriteLock();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 读文件到json
|
|
/// </summary>
|
|
/// <typeparam name="T">任意模型,根据模型匹配文件名</typeparam>
|
|
/// <returns></returns>
|
|
public string ReadDb<T>()
|
|
{
|
|
//using (BaseAccess access = new BaseAccess())
|
|
//{
|
|
// List<AgvInfo> lsit = access.Select<AgvInfo>();
|
|
//}
|
|
objlock.EnterReadLock();
|
|
try
|
|
{
|
|
string fileName = typeof(T).Name.ToString() + ".json";
|
|
//如果没有则创建文件夹
|
|
if (!Directory.Exists(xmlPath))
|
|
{
|
|
Directory.CreateDirectory(xmlPath);
|
|
}//如果没有则创建文件
|
|
if (!File.Exists(xmlPath + fileName))
|
|
{
|
|
FileStream fs1 = new FileStream(xmlPath + fileName, FileMode.Create, FileAccess.ReadWrite);
|
|
fs1.Close();
|
|
return null;
|
|
}
|
|
else
|
|
{
|
|
var temp = File.ReadAllText(xmlPath + fileName);
|
|
return temp;
|
|
//return JsonConvert.DeserializeObject<List<T>>(temp);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("read error!" + ex.ToString());
|
|
if (readcount < TryReadWriteCount)
|
|
{
|
|
Thread.Sleep(1);
|
|
readcount++;
|
|
ReadDb<T>();
|
|
}
|
|
|
|
//CreateLog.Log.Error("read error!" + ex.ToString());
|
|
return null;
|
|
}
|
|
finally
|
|
{
|
|
objlock.ExitReadLock();
|
|
}
|
|
}
|
|
}
|
|
}
|