76 lines
2.3 KiB
C#
76 lines
2.3 KiB
C#
using System.Text;
|
|
|
|
namespace YLErp
|
|
{
|
|
static class InnerHelper
|
|
{
|
|
public static readonly OptUserInfo UserInfo;
|
|
|
|
static InnerHelper()
|
|
{
|
|
UserInfo = OptUserInfo.SystemUser;
|
|
}
|
|
|
|
public static FileStream CreateTempFile(string fileName, out string fullFilePath)
|
|
{
|
|
var tempFolder = Path.Combine(AppContext.BaseDirectory, "temp");
|
|
|
|
Directory.CreateDirectory(tempFolder);
|
|
|
|
fullFilePath = Path.Combine(tempFolder, fileName);
|
|
|
|
return new FileStream(fullFilePath, FileMode.Create, FileAccess.ReadWrite);
|
|
}
|
|
|
|
public static T ReadSetting<T>(string fileName)
|
|
{
|
|
var filePath = GetFilePath(fileName);
|
|
if (File.Exists(filePath))
|
|
{
|
|
var json = File.ReadAllText(filePath);
|
|
return JsonHelper.Deserialize<T>(json);
|
|
}
|
|
return default;
|
|
}
|
|
|
|
public static void SaveSetting<T>(string fileName, T data)
|
|
{
|
|
var filePath = GetFilePath(fileName);
|
|
using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
|
|
using (var sw = new StreamWriter(fs, Encoding.UTF8))
|
|
{
|
|
var json = JsonHelper.Serialize(data);
|
|
sw.WriteLine(json);
|
|
}
|
|
}
|
|
|
|
public static string ReadSetting(string fileName)
|
|
{
|
|
var filePath = GetFilePath(fileName);
|
|
if (File.Exists(filePath))
|
|
{
|
|
return File.ReadAllText(filePath);
|
|
}
|
|
return string.Empty;
|
|
}
|
|
|
|
public static void SaveSetting(string fileName, string content)
|
|
{
|
|
var filePath = GetFilePath(fileName);
|
|
using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
|
|
using (var sw = new StreamWriter(fs, Encoding.UTF8))
|
|
{
|
|
sw.WriteLine(content);
|
|
}
|
|
}
|
|
|
|
private static string GetFilePath(string fileName)
|
|
{
|
|
var dataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
|
var baseFolder = Path.Combine(dataFolder, "YLTestTool");
|
|
Directory.CreateDirectory(baseFolder);
|
|
return Path.Combine(baseFolder, fileName);
|
|
}
|
|
}
|
|
}
|