• 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

  • 相关阅读:
    Datatables 行数据删除、行上升、行下降功能演示
    1.2 初识输入输出
    布局管理——绝对定位
    创建一个包括菜单栏,工具栏,状态栏,文本编辑部件的经典GUI应用程序的骨架
    工具栏
    上下文菜单
    选项菜单
    创建子菜单
    设置菜单栏
    添加状态栏(显示状态信息)
  • 原文地址:https://www.cnblogs.com/wangqc/p/iniReaderWrite.html
Copyright © 2020-2023  润新知