123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Reflection;
- using IniParser;
- using IniParser.Model;
- using Utilities;
- namespace SAGA.DotNetUtils.Utilities
- {
- public class IniOperator
- {
- private IniOperator()
- {
- if (!File.Exists(IniPath))
- {
- FileInfo fileInfo = new FileInfo(IniPath);
- if (!fileInfo.Exists)
- {
- var parentDir = fileInfo.Directory;
- if (parentDir != null)
- {
- if (!parentDir.Exists)
- {
- parentDir.Create();
- }
- var fileStream = File.Create(IniPath);
- fileStream.Dispose();
- }
- }
- }
- }
- private static string IniPath { get; set; }
- private static string DefaultPath = Path.Combine(AppBaseInfo.AppTempFilePath, "Settings\\Settings.ini");
- public static IniOperator Instance(string path)
- {
- IniPath = path;
- return new IniOperator();
- }
- public static IniOperator Instance()
- {
- return Instance(DefaultPath);
- }
- public void SetSetting(string key, string value)
- {
- SetData("Settings", key, value);
- }
- public string GetSetting(string key)
- {
- return GetData("Settings", key);
- }
- public void SetData(string section, string key, string value)
- {
- var parser = new FileIniDataParser();
- IniData data = parser.ReadFile(IniPath);
- if (string.IsNullOrEmpty(section) || string.IsNullOrEmpty(key)) return;
- data[section][key] = value + "";
- parser.WriteFile(IniPath, data);
- }
- /// <summary>
- /// 获取该部分下的所有Value
- /// </summary>
- /// <param name="section"></param>
- /// <returns></returns>
- public Dictionary<string, string> GetData(string section)
- {
- Dictionary<string, string> dic = new Dictionary<string, string>();
- var parser = new FileIniDataParser();
- IniData data = parser.ReadFile(IniPath);
- if (string.IsNullOrEmpty(section)) return dic;
- if (!data.Sections.ContainsSection(section)) return dic;
- data[section].ToList().ForEach(t => dic.Add(t.KeyName, t.Value));
- return dic;
- }
- public string GetData(string section, string key)
- {
- var parser = new FileIniDataParser();
- IniData data = parser.ReadFile(IniPath);
- if (string.IsNullOrEmpty(section) || string.IsNullOrEmpty(key)) return "";
- if (!data.Sections.ContainsSection(section) || !data[section].ContainsKey(key)) return "";
- return data[section][key];
- }
- public void SaveData<T>(T t)
- {
- var parser = new FileIniDataParser();
- IniData data = parser.ReadFile(IniPath);
- Type type = t.GetType();
- var className = type.Name;
- System.Reflection.PropertyInfo[] propertyInfos = type.GetProperties();
- foreach (var propertyInfo in propertyInfos)
- {
- var name = propertyInfo.Name;
- var value = propertyInfo.GetValue(t);
- //if (propertyInfo.PropertyType == typeof(List<string>))
- if (value is List<string> strList)
- {
- data[className][name] = string.Join(",", strList);
- }
- else
- {
- data[className][name] = value + "";
- }
- }
- parser.WriteFile(IniPath, data);
- }
- public T GetData<T>() where T : class, new()
- {
- var parser = new FileIniDataParser();
- IniData data = parser.ReadFile(IniPath);
- Type type = typeof(T);
- Assembly asm = type.Assembly;
- var obj = asm.CreateInstance(type.FullName);
- var className = type.Name;
- PropertyInfo[] propertyInfos = type.GetProperties();
- foreach (var propertyInfo in propertyInfos)
- {
- var name = propertyInfo.Name;
- object value = null;
- if (propertyInfo.PropertyType == typeof(List<string>))
- {
- List<string> d = new List<string>();
- string v = data[className][name];
- if (!string.IsNullOrEmpty(v))
- {
- d.AddRange(v.Split(','));
- value = d;
- }
- }
- else
- {
- value = data[className][name];
- }
- propertyInfo.SetValue(obj, value);
- }
- return obj as T;
- }
- }
- }
|