using CommonLib.LOG;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace CommonLib.IO
{
public class IniFile
{
private string m_Path = string.Empty;
public string Path {
get { return m_Path; }
set
{
if (value.Length>0&&value[0]=='.')
{
Assembly assembly = Assembly.GetEntryAssembly();
string exeFileFullName = assembly.Location;
string ExeDir = System.IO.Path.GetDirectoryName(exeFileFullName);
m_Path = ExeDir + value.Substring(1);
}
else
{
m_Path = value;
}
}
}
public IniFile(string path)
{
this.Path = path;
}
public IniFile()
{
this.Path = string.Empty;
}
///
///
///
/// 要读取的段落名
/// 要读取的键
/// 读取异常的情况下的缺省值
/// key所对应的值,如果该key不存在则返回空值
/// 值允许的大小
/// INI文件的完整路径和文件名
///
/*
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,
string key, string defVal, string retVal, int size, string filePath);
*/
///
/// section:要读取的段落名
///
///
/// 要读取的键
/// 读取异常的情况下的缺省值
/// 此参数类型不是string,而是Byte[] 用于返回byte类型的section组或键值组。
/// 值允许的大小
/// INI文件的完整路径和文件名
///
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,
string key, StringBuilder defVal, StringBuilder retVal, int size, string filePath);
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,
string key, string val, string filePath);
///
/// 写INI文件
///
/// 段落
/// 键
/// 值
public void IniWriteValue(string section, string key, string iValue)
{
if(!Directory.Exists(this.Path))
{
string dir = System.IO.Path.GetDirectoryName(this.Path);
Directory.CreateDirectory(dir);
}
WritePrivateProfileString(section, key, iValue, this.Path);
}
///
/// 读取INI文件
///
/// 段落
/// 键
/// 返回的键值
public string IniReadValue(string section, string key)
{
StringBuilder temp = new StringBuilder(255);
StringBuilder DefVal = new StringBuilder("") ;
//string temp = new string('\0', 255);
int i = GetPrivateProfileString(section, key, DefVal, temp, 255, this.Path);
return temp.ToString();
}
///
/// 获取成员变量的名字+
///
///
///
///
///
//string TableName = "123";
//string nameOfTestVariable = GetMemberName(() => TableName);
public static string GetMemberName(Expression> memberExpression)
{
MemberExpression expressionBody = (MemberExpression)memberExpression.Body;
return expressionBody.Member.Name;
}
///
/// 获取属性名称
///
///
///
///
/// Response.Write(GetPropertyName< TestClass >(p=>p.ID)) ; //输出的是 "ID" 两字母
public static string GetPropertyName(Expression> expr)
{
var rtn = "";
if (expr.Body is UnaryExpression)
{
rtn = ((MemberExpression)((UnaryExpression)expr.Body).Operand).Member.Name;
}
else if (expr.Body is MemberExpression)
{
rtn = ((MemberExpression)expr.Body).Member.Name;
}
else if (expr.Body is ParameterExpression)
{
rtn = ((ParameterExpression)expr.Body).Type.Name;
}
return rtn;
}
///
/// 保存Ini文件名
///
public Boolean SaveIniFile(string strFileName)
{
this.Path = strFileName;
return SaveIniFile();
}
public Boolean SaveIniFile()
{
Boolean result = true;
try
{
// 获得此模型的公共属性
var Fields = GetType().GetFields();
//遍历该对象的所有属性
foreach (var f in Fields)
{
//
Object obj = f.GetValue(this);
//将属性名称赋值给临时变量
string FieldName = f.Name;
var Properties = obj.GetType().GetProperties();
foreach (var p in Properties)
{
string PropertName = p.Name;
var pValue = p.GetValue(obj);
string strValue = pValue == null ? string.Empty : pValue.ToString();
IniWriteValue(FieldName, PropertName, strValue);
}
}
}
catch (Exception ex)
{
result = false;
LogStorage.Logs.Add(LogLevel.Error, ex.Message + "\n" + ex.StackTrace);
}
return result;
}
//通过反射获取变量名
public Boolean ReadIniFile(string FilePath)
{
Boolean bResult = false;
if (File.Exists(FilePath))
{
Path = FilePath;
try
{
Assembly assembly = Assembly.GetEntryAssembly();
//.GetExecutingAssembly(); // 获取当前程序集
// 获得此模型的公共属性
var Fields = GetType().GetFields();
//遍历该对象的所有属性
foreach (var f in Fields)
{
string strNamespace = f.FieldType.Namespace;
string fieldClassName = f.FieldType.Name;
string thisClassFullName = f.DeclaringType.FullName;
string thisFieldFullName = f.FieldType.FullName;
dynamic obj = assembly.CreateInstance(thisFieldFullName);
//将属性名称赋值给临时变量
string FieldName = f.Name;
var Properties = obj.GetType().GetProperties();
foreach (var p in Properties)
{
string PropertName = p.Name;
string pValue = IniReadValue(FieldName, PropertName );
p.SetValue(obj, pValue);
}
f.SetValue(this, obj);
}
bResult = true;
}
catch (Exception ex)
{
LogStorage.Logs.Add(LogLevel.Error, ex.Message + "\n" + ex.StackTrace);
}
}
return bResult;
}
}
}