修改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");