ini,config文件的读取修改

修改ini配置文件

// 声明INI文件的写操作函数 WritePrivateProfileString()

[System.Runtime.InteropServices.DllImport("kernel32")]

private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

// 声明INI文件的读操作函数 GetPrivateProfileString()

[System.Runtime.InteropServices.DllImport("kernel32")]

private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);

StringBuilder temp = new StringBuilder(255);
string FileName = FrmSelectPath.selectPath + "\dbconfig.ini";//ini文件路径
int i = GetPrivateProfileString("dbinfo", "database", "", temp, 255, FileName);
if (i != 0)
{
string keyValue = dbname;//键值
WritePrivateProfileString("dbinfo", "database", server, FileName);
WritePrivateProfileString("dbinfo", "user", username, FileName);
WritePrivateProfileString("dbinfo", "pwd", userpwd, FileName);
WritePrivateProfileString("Remort", "baseInfoUrl", @"tcp://"+Remoting+@"/BaseInfo", FileName);
WritePrivateProfileString("Remort", "commonUrl", @"tcp://"+Remoting+@"/Common", FileName);
WritePrivateProfileString("Remort", "Interface", @"tcp://"+Remoting+@"/", FileName);
}
else
{
MessageBox.Show("修改后台配置文件出错!");
}

修改config配置文件

//需要引用命名空间 using System.Xml;
xmldocument xdc = new xmldocument();
xdc.Load(FrmSelectPath.selectPath + "\FrontInterface.exe.config");
string sqlConnection = "Provider=SQLOLEDB;Data Source="+server+";Initial Catalog="+dbname+";Integrated Security=SSPI;";
foreach(XmlNode xnode in xdc["configuration"]["appSettings"].ChildNodes)
{
string keyStr = xnode.Attributes["key"].Value;
XmlElement xel =(XmlElement)xnode;
if(keyStr=="connString")
{
xel.SetAttribute("value",sqlConnection);
}
if(keyStr == "Interface1" || keyStr == "Interface" || keyStr == "InterfaceRun" )
{
xel.SetAttribute("value",@"tcp://"+Remoting+@"/");
}
}
xdc.Save(FrmSelectPath.selectPath+"\FrontInterface.exe.config");


作者:wangqc
出处:http://www.cnblogs.com/wangqc/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-wangqc

原文地址:https://www.cnblogs.com/wangqc/p/iniReaderWrite.html