1.概要
读取如下格式的配置文件
[A_0]
RunSwitch=1
X1=11
X2=a1
[A_1]
RunSwitch=1
X1=12
X2=a2
[B_0]
RunSwitch=1
X1=21
X2=b1
[B_1]
RunSwitch=1
X1=22
X2=b2
2.代码
2.1 Program 主函数类
using System;
using System.Collections.Generic;
namespace 配置文件读取试验
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("配置文件读取试验");
Program program = new Program();
program.view();
Console.ReadKey();
}
Dictionary<string, List<X>> keyValuePairs = new Dictionary<string, List<X>>();
Dictionary<string, Type> keyValuePairs2 = new Dictionary<string, Type>();
public Program()
{
keyValuePairs2.Add("A", typeof(A));
keyValuePairs2.Add("B", typeof(B));
foreach (KeyValuePair<string, Type> keyValuePair in keyValuePairs2)
{
keyValuePairs.Add(keyValuePair.Key, new List<X>());
for (int i = 0; i < 2; i++)
{
object[] index = { i };
X x = (X)keyValuePair.Value.GetConstructors()[0].Invoke(index);
keyValuePairs[keyValuePair.Key].Add(x);
}
}
}
private void view()
{
foreach(KeyValuePair<string, List<X>> keyValuePair in keyValuePairs)
{
foreach(X x in keyValuePair.Value)
{
x.view();
}
}
}
}
class X
{
private readonly string key;
private int index = 0;
int RunSwitch = 0;
public X(string key, int index)
{
this.key = key;
this.index = index;
IniParamReadBase(key, index);
}
protected void IniParamReadBase(string strDev, int iIndex)
{
FileIniReadValuer fileIniReadValuer = new FileIniReadValuer(strDev, iIndex);
RunSwitch = fileIniReadValuer.GetInt("RunSwitch");
X1 = fileIniReadValuer.GetInt("X1");
//ModbusServerUrl
X2 = fileIniReadValuer.GetStr("X2");
}
public void view()
{
Console.WriteLine("\n"+key+"_"+index);
Console.WriteLine("RunSwitch:" + RunSwitch);
Console.WriteLine("X1:"+ X1);
Console.WriteLine("X2:" + X2);
}
protected int X1;
protected string X2;
}
class A: X
{
public A(int index):base("A", index)
{
}
}
class B : X
{
public B(int index) : base("B", index)
{
}
}
}
2.2 INIFile(配置文件读取类)
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace 配置文件读取试验
{
/// <summary>
/// INI文件读写类。
/// </summary>
public class INIFile
{
public string path;
public INIFile(string INIPath)
{
path = INIPath;
ExistINIFile();
}
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath);
/// <summary>
/// 写INI文件
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="Value"></param>
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.path);
}
/// <summary>
/// 读取INI文件
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <returns></returns>
public string IniReadValue(string Section, string Key)
{
var temp = new StringBuilder(500);
var i = GetPrivateProfileString(Section, Key, "-1", temp, 500, this.path);
return temp.ToString();
}
public byte[] IniReadValues(string section, string key)
{
var temp = new byte[255];
var i = GetPrivateProfileString(section, key, "-1", temp, 255, this.path);
return temp;
}
/// <summary>
/// 删除ini文件下所有段落
/// </summary>
public void ClearAllSection()
{
IniWriteValue(null, null, null);
}
/// <summary>
/// 删除ini文件下personal段落下的所有键
/// </summary>
/// <param name="Section"></param>
public void ClearSection(string Section)
{
IniWriteValue(Section, null, null);
}
public bool ExistINIFile()
{
return File.Exists(this.path);
}
}
}
2.3 FileIniReadValuer(配置文件某一段的读取类)
using System;
namespace 配置文件读取试验
{
/// <summary>
/// 读取参数
/// </summary>
public class FileIniReadValuer
{
string strSectionName;
INIFile file;
public FileIniReadValuer(string strDev, int iIndex)
{
var strPath = ".//设置参数//ScmSet.ini";
file = new INIFile(strPath);
//段名
strSectionName = strDev + "_" + iIndex.ToString();
}
public string GetStr(string strKeyName)
{
string str = file.IniReadValue(strSectionName, strKeyName);
return str;
}
public int GetInt(string strKeyName)
{
string str = file.IniReadValue(strSectionName, strKeyName);
return Convert.ToInt16(str);
}
}
}
3.运行结果