本文介绍: 一般我们代码生成了之后,就不会动了。而可动的参数一般写在配置文件里面。配置语言的格式一般有一下几种优点缺点xml扩展性强,歧义性小对于人来说过于冗长Json可读性强无法添加注释yaml可读取强缩进地狱,手动修改时极其容易出现问题set;set;getset;if (!/// 创建文件tryRead();throw new Exception(“文件读取失败!请确认是否配置文件格式是否正确”);/// 覆盖文件。
代码
public class MyXmlConfigAutoHelper<T>
{
public T Setting { get; set; }
public string FileName { get; set; } = "MyConfig.xml";
public string BackupName
{
get
{
var regex = new Regex(@"(w+).(w+)$");
var filename = regex.Match(FileName).Value;
var backName = (new Regex(@".(w+)$")).Split(filename)[0];
var newBackName = backName + "_back";
return (new Regex(backName)).Replace(FileName, newBackName);
}
}
/// <summary>
/// 备份
/// </summary>
private MyXmlConfigHelper<T> backupXml { get; set; }
private MyXmlConfigHelper<T> settingXml { get; set; }
public MyXmlConfigAutoHelper()
{
}
public MyXmlConfigAutoHelper(string fileName)
{
FileName = fileName;
}
public MyXmlConfigAutoHelper(string fileName, T setting)
{
Setting = setting;
FileName = fileName;
}
/// <summary>
/// 实例化
/// </summary>
public void AutoInit()
{
settingXml = new MyXmlConfigHelper<T>(Setting, FileName);
backupXml = new MyXmlConfigHelper<T>(Setting, BackupName);
//如果备份也损坏了,就GG了
var settingFlag = true;
var backupFlag = true;
try
{
settingXml.Init();
}
catch (Exception ex)
{
Console.WriteLine("主文件读取失败");
Console.WriteLine(ex.Message);
settingFlag = false;
}
try
{
backupXml.Init();
}
catch (Exception ex)
{
Console.WriteLine("备份文件读取失败");
Console.WriteLine(ex.Message);
backupFlag = false;
}
Console.WriteLine($"文件完整性:setting[{settingFlag},backup[{backupFlag}]]");
if (!backupFlag && !settingFlag)
{
Console.WriteLine("主要和备份文件完全破损,默认值覆盖");
backupXml.ReInit();
settingXml.ReInit();
}
else if (!backupFlag)
{
Console.WriteLine("备份文件完全破损,主要文件覆盖");
backupXml.Setting = settingXml.Setting;
backupXml.ReInit();
}
else if (!settingFlag)
{
Console.WriteLine("主要文件完全破损,备份文件覆盖");
settingXml.Setting = backupXml.Setting;
settingXml.ReInit();
}
else
{
Console.WriteLine("主要和备份文件正常,主要文件覆盖备份文件");
backupXml.Setting = settingXml.Setting;
backupXml.ReInit();
}
Setting = settingXml.Setting;
}
public void ReInit()
{
settingXml = new MyXmlConfigHelper<T>(Setting, FileName);
backupXml = new MyXmlConfigHelper<T>(Setting, BackupName);
settingXml.ReInit();
backupXml.ReInit();
}
}
测试用例
static void Main(string[] args)
{
var config = new MyConfigService() {
Name = "小坤",
Description="爱坤",
Default = "鲲鲲",
SettingKey = MyConfigService.MyKeys.Banana,
Id = 80086,
IsEnabled = true,
};
var xmlAutoHelper = new MyXmlConfigAutoHelper<MyConfigService>("resource\Myconfig.xml", config);
xmlAutoHelper.AutoInit();
//Console.WriteLine(xmlAutoHelper.BackupName);
Console.WriteLine("运行完成!");
Console.ReadKey();
}
运行结果
由于测试步骤过于复杂,情况比较多,这里就不放截图了。简单来说就是尽可能的使用已有的数据进行还原,如果两个文件都损坏直接使用默认值替换
代码封装总结
public class MyXmlConfigAutoHelper<T>
{
public T Setting { get; set; }
public string FileName { get; set; } = "MyConfig.xml";
public string BackupName
{
get
{
var regex = new Regex(@"(w+).(w+)$");
var filename = regex.Match(FileName).Value;
var backName = (new Regex(@".(w+)$")).Split(filename)[0];
var newBackName = backName + "_back";
return (new Regex(backName)).Replace(FileName, newBackName);
}
}
/// <summary>
/// 备份
/// </summary>
private MyXmlConfigHelper<T> backupXml { get; set; }
private MyXmlConfigHelper<T> settingXml { get; set; }
public MyXmlConfigAutoHelper()
{
}
public MyXmlConfigAutoHelper(string fileName)
{
FileName = fileName;
}
public MyXmlConfigAutoHelper(string fileName, T setting)
{
Setting = setting;
FileName = fileName;
}
/// <summary>
/// 实例化
/// </summary>
public void AutoInit()
{
settingXml = new MyXmlConfigHelper<T>(Setting, FileName);
backupXml = new MyXmlConfigHelper<T>(Setting, BackupName);
//如果备份也损坏了,就GG了
var settingFlag = true;
var backupFlag = true;
try
{
settingXml.Init();
}
catch (Exception ex)
{
Console.WriteLine("主文件读取失败");
Console.WriteLine(ex.Message);
settingFlag = false;
}
try
{
backupXml.Init();
}
catch (Exception ex)
{
Console.WriteLine("备份文件读取失败");
Console.WriteLine(ex.Message);
backupFlag = false;
}
Console.WriteLine($"文件完整性:setting[{settingFlag},backup[{backupFlag}]]");
if (!backupFlag && !settingFlag)
{
Console.WriteLine("主要和备份文件完全破损,默认值覆盖");
backupXml.ReInit();
settingXml.ReInit();
}
else if (!backupFlag)
{
Console.WriteLine("备份文件完全破损,主要文件覆盖");
backupXml.Setting = settingXml.Setting;
backupXml.ReInit();
}
else if (!settingFlag)
{
Console.WriteLine("主要文件完全破损,备份文件覆盖");
settingXml.Setting = backupXml.Setting;
settingXml.ReInit();
}
else
{
Console.WriteLine("主要和备份文件正常,主要文件覆盖备份文件");
backupXml.Setting = settingXml.Setting;
backupXml.ReInit();
}
Setting = settingXml.Setting;
}
public void ReInit()
{
settingXml = new MyXmlConfigHelper<T>(Setting, FileName);
backupXml = new MyXmlConfigHelper<T>(Setting, BackupName);
settingXml.ReInit();
backupXml.ReInit();
}
}
public class MyXmlConfigHelper<T>
{
public T Setting { get; set; }
public string FileName { get; set; } = "MyConfig.xml";
public string DirectoryPath
{
get
{
var regex = new Regex(@"\(w+).(w+)$");
return regex.Split(FullPath)[0];
}
}
public string DebugPath { get => Directory.GetCurrentDirectory(); }
public string FullPath { get => DebugPath + "\" + FileName; }
public bool IsFileExist { get => File.Exists(FullPath); }
public bool IsDirectoryExist { get => Directory.Exists(DirectoryPath); }
public Action<string> ShowMsg { get; set; } = (msg) => Console.WriteLine(msg);
public MyXmlConfigHelper()
{
}
public MyXmlConfigHelper(string filename)
{
FileName = filename;
if (!IsDirectoryExist)
{
DirectoryInfo directoryInfo = new DirectoryInfo(DirectoryPath);
directoryInfo.Create();
}
}
public MyXmlConfigHelper(T setting, string filename) : this(filename)
{
Setting = setting;
}
/// <summary>
/// 创建文件
/// </summary>
public void Init()
{
if (IsFileExist)
{
try
{
Read();
}
catch (Exception ex)
{
ShowMsg(ex.ToString());
throw new Exception("文件读取失败!请确认是否配置文件格式是否正确");
}
}
else
{
Write();
}
}
/// <summary>
/// 覆盖文件
/// </summary>
public void ReInit()
{
ShowMsg("正在覆盖配置文件:" + FullPath);
Write();
}
/// <summary>
/// 写入配置类
/// </summary>
public void Write()
{
ShowMsg("正在生成配置文件:" + FullPath);
var xmlHelper = new XmlSerializer(typeof(T));
using (StreamWriter xmlWriter = new StreamWriter(FullPath))
{
//去掉烦人的命名空间
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
xmlHelper.Serialize(xmlWriter, Setting, ns);
xmlWriter.Close();
}
}
/// <summary>
/// 读取配置类
/// </summary>
public void Read()
{
ShowMsg("正在读取配置文件:" + FullPath);
var xmlHelper = new XmlSerializer(typeof(T));
using (StreamReader xmlReader = new StreamReader(FullPath))
{
Setting = (T)xmlHelper.Deserialize(xmlReader);
xmlReader.Close();
}
}
}
总结
我这里最后加了个back备份文件,我们平时就修改主要文件的配置即可。如果主要文件损坏,那就备份文件补上。但是这个是主要文件损坏的情况,如果主要文件没损坏,是参数设置错了呢?那我们可以自动生成按照时间戳的备份文件,一次存多个。
- 主要文件
- 备份文件-2024-1-6 17:37:20
- 备份文件-2024-1-6 17:37:30
为了安全考虑的方式是没有上限的。这里就不展开说明了,这里已经写好一个基本的设置文件自动保存,和设置文件自动备份回档的功能,如果想要更高的安全基本可以自己在我的代码上面继续封装。
还有备份文件可以当做缓存文件一样来使用,但是这个是明文存储的。可以自己手动加密一下,反正加密和解密的方法也有很多。可以自己琢磨一下。
原文地址:https://blog.csdn.net/qq_44695769/article/details/135425779
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_53746.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。