引用都是一些基础引用,自己添加就行。凑合着可用,请自行根据情况优化。注意:使用时请注意编码问题
/// <summary>
/// 获取配置文件
/// </summary>
/// <param name="path">路径</param>
/// <param name="group">组名</param>
/// <param name="key">键</param>
/// <returns>值</returns>
public static string GetConfigValue(string path,string group,string key)
{
if (!File.Exists(path)) return null;
group =string.Concat("[" , group , "]");
StreamReader sr = new StreamReader(path,Encoding.Default);//使用时请注意字符编码问题
bool group_bl = false;
string value = null;
while(!sr.EndOfStream)
{
string line = sr.ReadLine().Trim();
if (string.IsNullOrEmpty(line)) continue;
if (line[0] == '[')
{
if(group_bl)
{
break;
}
if (line == group)
{
group_bl = true;
continue;
}
}
if(group_bl)
{
string[] strkv = line.Split('=');
if (strkv.Length < 2) continue;
if(strkv[0].Trim()==key)
{
value= strkv[1];
break;
}
}
}
sr.Close();
sr.Dispose();
return value.Trim();
}
public static Dictionary<string,string> GetConfigValue(string path, string group)
{
if (!File.Exists(path)) return null;
group = string.Concat("[", group, "]");
StreamReader sr = new StreamReader(path, Encoding.Default);//使用时请注意字符编码问题
bool group_bl = false;
Dictionary<string, string> result = new Dictionary<string, string>();
while (!sr.EndOfStream)
{
string line = sr.ReadLine().Trim();
if (string.IsNullOrEmpty(line)) continue;
if (line[0] == '[')
{
if (group_bl)
{
break;
}
if (line == group)
{
group_bl = true;
continue;
}
}
if (group_bl)
{
string[] strkv = line.Split('=');
if (strkv.Length < 2) continue;
result[strkv[0].Trim()] = strkv[1].Trim();
}
}
sr.Close();
sr.Dispose();
return result;
}
public static Dictionary<string,Dictionary<string, string>> GetConfigValue(string path)
{
if (!File.Exists(path)) return null;
StreamReader sr = new StreamReader(path, Encoding.Default);//使用时请注意字符编码问题
Dictionary<string, Dictionary<string, string>> result = new Dictionary<string, Dictionary<string, string>>();
Dictionary<string, string> curgroup=null;
while (!sr.EndOfStream)
{
string line = sr.ReadLine().Trim();
if (string.IsNullOrEmpty(line)) continue;
if (line[0] == '[' && line[line.Length - 1] == ']')
{
curgroup = new Dictionary<string, string>();
string key = line.Remove(line.Length - 1).Substring(1);
result[key] = curgroup;
continue;
}
if(curgroup!=null)
{
string[] strkv = line.Split('=');
if (strkv.Length < 2) continue;
curgroup[strkv[0].Trim()] = strkv[1].Trim();
}
}
sr.Close();
sr.Dispose();
return result;
}
/// <summary>
/// 设置配置文件
/// </summary>
/// <param name="path">路径</param>
/// <param name="group">组</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
public static void SetConfigValue(string path, string group, string key,string value)
{
group = string.Concat("[" , group ,"]");
Dictionary<string, Dictionary<string, string>> gkv = new Dictionary<string, Dictionary<string, string>>();
if (File.Exists(path))
{
StreamReader sr = new StreamReader(path, Encoding.Default);//使用时请注意字符编码问题
string curgroup = null;
while (!sr.EndOfStream)
{
string line = sr.ReadLine().Trim();
if (string.IsNullOrEmpty(line))
{
continue;
}
if (line[0] == '['&&line[line.Length-1]==']')
{
curgroup = line;
gkv[curgroup] = new Dictionary<string, string>();
continue;
}
if(curgroup!=null)
{
string[] strkv = line.Split('=');
if(strkv.Length>=2)
{
gkv[curgroup][strkv[0]] = strkv[1];
}
else
{
gkv[curgroup][strkv[0]] = string.Empty;
}
}
}
sr.Close();
sr.Dispose();
}
if(!gkv.ContainsKey(group))
{
gkv[group] = new Dictionary<string, string>();
}
gkv[group][key] = value;
using (StreamWriter sw = new StreamWriter(path, false, Encoding.Default))
{
foreach (var temp in gkv)
{
sw.WriteLine(temp.Key);
foreach (var temp1 in temp.Value)
{
sw.WriteLine("{0}={1}",temp1.Key,temp1.Value);
}
}
}
}
public static void SetConfigValue(string path, string group, Dictionary<string,string> pairs)
{
group = string.Concat("[", group, "]");
Dictionary<string, Dictionary<string, string>> gkv = new Dictionary<string, Dictionary<string, string>>();
if (File.Exists(path))
{
StreamReader sr = new StreamReader(path, Encoding.Default);//使用时请注意字符编码问题
string curgroup = null;
while (!sr.EndOfStream)
{
string line = sr.ReadLine().Trim();
if (string.IsNullOrEmpty(line))
{
continue;
}
if (line[0] == '[' && line[line.Length - 1] == ']')
{
curgroup = line;
gkv[curgroup] = new Dictionary<string, string>();
continue;
}
if (curgroup != null)
{
string[] strkv = line.Split('=');
if (strkv.Length >= 2)
{
gkv[curgroup][strkv[0]] = strkv[1];
}
else
{
gkv[curgroup][strkv[0]] = string.Empty;
}
}
}
sr.Close();
sr.Dispose();
}
if (gkv.ContainsKey(group))
{
foreach(var pair in pairs)
{
gkv[group][pair.Key] = pair.Value;
}
}
else
{
gkv[group] = pairs;
}
using (StreamWriter sw = new StreamWriter(path, false, Encoding.Default))
{
foreach (var temp in gkv)
{
sw.WriteLine(temp.Key);
foreach (var temp1 in temp.Value)
{
sw.WriteLine("{0}={1}", temp1.Key, temp1.Value);
}
}
}
}
public static void SetConfigValue(string path, Dictionary<string,Dictionary<string, string>> pairs)
{
StringBuilder stb = new StringBuilder();
Dictionary<string, Dictionary<string, string>> gkv = new Dictionary<string, Dictionary<string, string>>();
if (File.Exists(path))
{
StreamReader sr = new StreamReader(path, Encoding.Default);//使用时请注意字符编码问题
string curgroup = null;
while (!sr.EndOfStream)
{
string line = sr.ReadLine().Trim();
if (string.IsNullOrEmpty(line))
{
continue;
}
if (line[0] == '[' && line[line.Length - 1] == ']')
{
curgroup = line;
gkv[curgroup] = new Dictionary<string, string>();
continue;
}
if (curgroup != null)
{
string[] strkv = line.Split('=');
if (strkv.Length == 2)
{
gkv[curgroup][strkv[0]] = strkv[1];
}
else
{
gkv[curgroup][strkv[0]] = string.Empty;
}
}
}
sr.Close();
sr.Dispose();
}
if (gkv.Count>0)
{
foreach(var pair in pairs)
{
if(gkv.ContainsKey(pair.Key))
{
foreach(var pair1 in pair.Value)
{
gkv[pair.Key][pair1.Key] = pair1.Value;
}
}
else
{
gkv[pair.Key] = pair.Value;
}
}
}
else
{
gkv.Clear();
gkv = pairs;
}
using (StreamWriter sw = new StreamWriter(path, false, Encoding.Default))
{
foreach (var temp in gkv)
{
sw.WriteLine(temp.Key);
foreach (var temp1 in temp.Value)
{
sw.WriteLine("{0}={1}", temp1.Key, temp1.Value);
}
}
}
}