作应用系统开发时,管理配置是必不可少的。例如数据库服务器的配置、安装和更新配置等等。由于Xml的兴起,现在的配置文件大都是以xml文档来存储。比如Visual Studio.Net自身的配置文件Mashine.config,Asp.Net的配置文件Web.Config,都是xml的格式。
传统的配置文件ini已有被xml文件逐步代替的趋势,但对于简单的配置,ini文件还是有用武之地的,尤其现在绿色软件的流行,越来越多的程序将自己的一些配置信息保存到了INI文件中。ini文件其实就是一个文本文件,它有固定的格式,节Section的名字用[]括起来,然后换行说明key的值:
[section]
key=value
读取ini
try
{
StreamReader sr = new StreamReader("game.ini", Encoding.GetEncoding("gb2312"));
if(sr.Peek() >= 0)
{
txtIP.Text = sr.ReadLine();
txtPort.Text=sr.ReadLine();
txtAppName.Text=sr.ReadLine();
txtPassword.Text=sr.ReadLine();
txtDBConnet.Text = sr.ReadLine();
GameWinRateUpDown.Text = sr.ReadLine();
KillRateUpDown.Text = sr.ReadLine();
}
sr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
写ini文件
try
{
IP = txtIP.Text;
Port = txtPort.Text;
AppName = txtAppName.Text;
Password = txtPassword.Text;
DBConnectString = txtDBConnet.Text;
gameWinRateUpDown = GameWinRateUpDown.Text;
killRateUpDown = KillRateUpDown.Text;
StreamWriter sw = new StreamWriter("game.ini", false, Encoding.GetEncoding("gb2312"));
sw.Flush();
sw.WriteLine(IP);
sw.WriteLine(Port);
sw.WriteLine(AppName);
sw.WriteLine(Password);
sw.WriteLine(DBConnectString);
sw.WriteLine(gameWinRateUpDown);
sw.WriteLine(killRateUpDown);
sw.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
// MessageBox.Show("配置文件路径不对!");
}
来源:https://www.cnblogs.com/ewaterlong/archive/2009/03/31/1425608.html