|
@@ -8,44 +8,64 @@ using IniParser;
|
|
|
using IniParser.Model;
|
|
|
using Utilities;
|
|
|
|
|
|
-namespace SAGA.DotNetUtils.Utilities {
|
|
|
- public static class IniOperator {
|
|
|
- static IniOperator() {
|
|
|
- if (!File.Exists(IniPath)) {
|
|
|
- Directory.CreateDirectory(Path.Combine(AppBaseInfo.AppTempFilePath, "Settings"));
|
|
|
- var fileStream = File.Create(IniPath);
|
|
|
- fileStream.Dispose();
|
|
|
+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 = Path.Combine(AppBaseInfo.AppTempFilePath, "Settings\\Settings.ini");
|
|
|
-
|
|
|
- public static void SaveTemp(string key, string value) {
|
|
|
- SetData("TempData", key, value);
|
|
|
- }
|
|
|
+ private static string IniPath { get; set; }
|
|
|
+ private static string DefaultPath = Path.Combine(AppBaseInfo.AppTempFilePath, "Settings\\Settings.ini");
|
|
|
|
|
|
- public static string GetTempData(string key) {
|
|
|
- return GetData("TempData", key);
|
|
|
+ public static IniOperator Instance(string path)
|
|
|
+ {
|
|
|
+ IniPath = path;
|
|
|
+ return new IniOperator();
|
|
|
}
|
|
|
|
|
|
- public static string GetRevtiDir() {
|
|
|
- return GetData("ComputerSetting", "RevitDir");
|
|
|
+ public static IniOperator Instance()
|
|
|
+ {
|
|
|
+ return Instance(DefaultPath);
|
|
|
}
|
|
|
|
|
|
- public static void SetSetting(string key, string value) {
|
|
|
+
|
|
|
+
|
|
|
+ public void SetSetting(string key, string value)
|
|
|
+ {
|
|
|
SetData("Settings", key, value);
|
|
|
}
|
|
|
- public static string GetSetting(string key) {
|
|
|
+ public string GetSetting(string key)
|
|
|
+ {
|
|
|
return GetData("Settings", key);
|
|
|
}
|
|
|
|
|
|
- public static void SetData(string section, string key, string value) {
|
|
|
+ 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;
|
|
|
+ if (string.IsNullOrEmpty(section) || string.IsNullOrEmpty(key)) return;
|
|
|
|
|
|
|
|
|
data[section][key] = value + "";
|
|
|
-
|
|
|
+
|
|
|
parser.WriteFile(IniPath, data);
|
|
|
}
|
|
|
/// <summary>
|
|
@@ -53,9 +73,9 @@ namespace SAGA.DotNetUtils.Utilities {
|
|
|
/// </summary>
|
|
|
/// <param name="section"></param>
|
|
|
/// <returns></returns>
|
|
|
- public static Dictionary<string,string> GetData(string section)
|
|
|
+ public Dictionary<string, string> GetData(string section)
|
|
|
{
|
|
|
- Dictionary<string,string> dic=new Dictionary<string, string>();
|
|
|
+ Dictionary<string, string> dic = new Dictionary<string, string>();
|
|
|
var parser = new FileIniDataParser();
|
|
|
IniData data = parser.ReadFile(IniPath);
|
|
|
if (string.IsNullOrEmpty(section)) return dic;
|
|
@@ -64,7 +84,8 @@ namespace SAGA.DotNetUtils.Utilities {
|
|
|
return dic;
|
|
|
}
|
|
|
|
|
|
- public static string GetData(string section, string key) {
|
|
|
+ public string GetData(string section, string key)
|
|
|
+ {
|
|
|
var parser = new FileIniDataParser();
|
|
|
IniData data = parser.ReadFile(IniPath);
|
|
|
if (string.IsNullOrEmpty(section) || string.IsNullOrEmpty(key)) return "";
|
|
@@ -72,20 +93,24 @@ namespace SAGA.DotNetUtils.Utilities {
|
|
|
return data[section][key];
|
|
|
}
|
|
|
|
|
|
- public static void SaveData<T>(T t) {
|
|
|
+ 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) {
|
|
|
+ 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) {
|
|
|
+ if (value is List<string> strList)
|
|
|
+ {
|
|
|
data[className][name] = string.Join(",", strList);
|
|
|
}
|
|
|
- else {
|
|
|
+ else
|
|
|
+ {
|
|
|
data[className][name] = value + "";
|
|
|
}
|
|
|
|
|
@@ -93,7 +118,7 @@ namespace SAGA.DotNetUtils.Utilities {
|
|
|
parser.WriteFile(IniPath, data);
|
|
|
}
|
|
|
|
|
|
- public static T GetData<T>() where T:class,new()
|
|
|
+ public T GetData<T>() where T : class, new()
|
|
|
{
|
|
|
var parser = new FileIniDataParser();
|
|
|
IniData data = parser.ReadFile(IniPath);
|
|
@@ -101,7 +126,7 @@ namespace SAGA.DotNetUtils.Utilities {
|
|
|
Assembly asm = type.Assembly;
|
|
|
var obj = asm.CreateInstance(type.FullName);
|
|
|
var className = type.Name;
|
|
|
- PropertyInfo[] propertyInfos = type.GetProperties();
|
|
|
+ PropertyInfo[] propertyInfos = type.GetProperties();
|
|
|
foreach (var propertyInfo in propertyInfos)
|
|
|
{
|
|
|
var name = propertyInfo.Name;
|
|
@@ -109,22 +134,22 @@ namespace SAGA.DotNetUtils.Utilities {
|
|
|
|
|
|
if (propertyInfo.PropertyType == typeof(List<string>))
|
|
|
{
|
|
|
- List<string> d=new 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];
|
|
|
+ value = data[className][name];
|
|
|
}
|
|
|
propertyInfo.SetValue(obj, value);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return obj as T;
|
|
|
}
|
|
|
}
|