c#读取及写入配置文件相对来说比较简单,这里以WPF为例,读取ini配置文件,首先大家先添加一个名为“ConfigLoad.cs”的类文件,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace wpf读取ini配置文件
{
class ConfigLoad
{
public ConfigLoad(string conFilePath)
{
this.conFilePath = conFilePath;
ReadConfig();
}
///
/// 配置文件的目录
///
#region ConFilePath
string conFilePath;
public string ConFilePath
{
set { conFilePath = value; }
get { return conFilePath; }
}
#endregion
///
/// 配置文件属性值
///
private List configName = new List();//名称集合
private List configValue = new List(); //数值集合
///
/// 读取配置文件的属性值
///
public bool ReadConfig()
{
//检查配置文件是否存在
if (!File.Exists(this.conFilePath))
{
return false;
}
StreamReader sr = new StreamReader(this.conFilePath, Encoding.Default);
string line;
while ((line = sr.ReadLine()) != null)
{
line = line.Trim();
string cName, cValue;
string[] cLine = line.Split('=');
if (cLine.Length == 2)
{
cName = cLine[0].ToLower();
cValue = cLine[1].ToLower();
configName.Add(cName);
configValue.Add(cValue);
}
}
sr.Close();
return true;
}
#region GetConfigValue
///
/// 返回变量的字符串值
///
/// 变量名称
/// 变量值
public string GetStringValue(string cName)
{
for (int i = 0; i < configName.Count; i++)
{
if (configName[i].Equals(cName.ToLower()))
{
return configValue[i];
}
}
return null;
}
public int GetIntValue(string cName)
{
for (int i = 0; i < configName.Count; i++)
{
if (configName[i].Equals(cName.ToLower()))
{
int result;
if (int.TryParse(configValue[i], out result))
{
return result;
}
}
}
return 0;
}
public float GetFloatValue(string cName)
{
for (int i = 0; i < configName.Count; i++)
{
if (configName[i].Equals(cName.ToLower()))
{
float result;
if (float.TryParse(configValue[i], out result))
{
return result;
}
}
}
return 0;
}
#endregion
}
public class ConfigSet
{
public ConfigSet(string configFilePath, bool isRead)
{
this.configFilePath = configFilePath;
if (File.Exists(this.configFilePath) && isRead)
{
ReadConfig();
}
}
public ConfigSet(string configFilePath)
{
this.configFilePath = configFilePath;
}
private string configFilePath;
private List configName = new List();//名称集合
private List configValue = new List(); //数值集合
///
/// 设置写入配置文件的值
///
/// 属性名称
/// 值
public void SetConfigValue(string cName, string cValue)
{
bool ishere = false;
//检查是否已经存在.
if (configName.Count != 0)
{
for (int i = 0; i < configName.Count; i++)
{
if (configName[i].Equals(cName.ToLower()))
{
configValue[i] = cValue;
ishere = true;
}
}
}
if (!ishere)
{
configName.Add(cName);
configValue.Add(cValue);
}
}
///
/// 将设置的值写入到ini文件中.
///
/// 是否追加到文件
///
public bool WriteConfigToFile(ConfigFile cf)
{
StreamWriter sw;
switch (cf)
{
case ConfigFile.newFile:
{
sw = new StreamWriter(this.configFilePath, false);
break;
};
case ConfigFile.appendFile:
{
sw = new StreamWriter(this.configFilePath, true);
break;
}
default:
{
sw = new StreamWriter(this.configFilePath);
break;
}
}
try
{
for (int i = 0; i < configName.Count; i++)
{
sw.WriteLine("{0}={1}", configName[i].ToLower(), configValue[i]);
}
}
catch
{
return false;
}
finally
{
sw.Close();
}
return true;
}
///
/// 读取配置文件的属性值
///
private bool ReadConfig()
{
StreamReader sr = new StreamReader(this.configFilePath, Encoding.Default);
string line;
while ((line = sr.ReadLine()) != null)
{
line = line.Trim();
string cName, cValue;
string[] cLine = line.Split('=');
if (cLine.Length == 2)
{
cName = cLine[0].ToLower();
cValue = cLine[1].ToLower();
configName.Add(cName);
configValue.Add(cValue);
}
}
sr.Close();
return true;
}
}
///
/// 写入文件属性
///
public enum ConfigFile { newFile, appendFile }
}
然后我们再MainWindow里面添加三个Label控件、三个TextBox控件和一个Button控件,代码如下:
最后我们在后台编写读取和写入ini配置文件的代码,代码如下:
///
///读取配置文件
///
///
private void ReadURL()
{
//设置配置文件
ConfigLoad con = new ConfigLoad("config.ini");
//textbox读取配置文件默认值
textBox1.Text = con.GetStringValue("url1");
textBox2.Text = con.GetStringValue("url2");
textBox3.Text = con.GetStringValue("url3");
}
///
///写入配置文件
///
///
private void Write()
{
try
{
ConfigSet cs = new ConfigSet("config.ini", true);
cs.SetConfigValue("url1", textBox1.Text.ToString());
cs.SetConfigValue("url2", textBox2.Text.ToString());
cs.SetConfigValue("url3", textBox3.Text.ToString());
cs.WriteConfigToFile(ConfigFile.newFile);
MessageBox.Show("修改配置文件URL成功");
}
catch(Exception e)
{
MessageBox.Show("修改配置文件URL失败");
}
finally
{
}
}