zoukankan      html  css  js  c++  java
  • c# ini文件操作类(简单配置文件)

    简单项目中可使用ini做为配置文件,操作类如下。

     1     public class INIClass
    2 {
    3 public string inipath;
    4 [System.Runtime.InteropServices.DllImport("kernel32")]
    5 private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
    6 [System.Runtime.InteropServices.DllImport("kernel32")]
    7 private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
    8 /// <summary>
    9 /// 构造方法
    10 /// </summary>
    11 /// <param name="INIPath">文件路径</param>
    12 public INIClass(string INIPath)
    13 {
    14 inipath = INIPath;
    15 }
    16 /// <summary>
    17 /// 写入INI文件
    18 /// </summary>
    19 /// <param name="Section">项目名称(如 [TypeName] )</param>
    20 /// <param name="Key"></param>
    21 /// <param name="Value"></param>
    22 public void IniWriteValue(string Section, string Key, string Value)
    23 {
    24 WritePrivateProfileString(Section, Key, Value, this.inipath);
    25 }
    26 /// <summary>
    27 /// 读出INI文件
    28 /// </summary>
    29 /// <param name="Section">项目名称(如 [TypeName] )</param>
    30 /// <param name="Key"></param>
    31 public string IniReadValue(string Section, string Key)
    32 {
    33 StringBuilder temp = new StringBuilder(500);
    34 int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
    35 return temp.ToString();
    36 }
    37 /// <summary>
    38 /// 验证文件是否存在
    39 /// </summary>
    40 /// <returns>布尔值</returns>
    41 public bool ExistINIFile()
    42 {
    43 return File.Exists(inipath);
    44 }
    45 }

    使用方法为:

                INIClass ini=new INIClass(System.Windows.Forms.Application.StartupPath + "\\config.ini");
    m_parentDirPath = ini.IniReadValue("road", "dir").Trim(); //读取
           ini.IniWriteValue("road", "dir", "d:\\aa"); //写入

    对应ini文件内容为:

    [road]
    dir=D:\lzx\data


    参考:

    ini文件

    ini配置文件读取类



  • 相关阅读:
    Linux strace 命令 说明
    存储区域网(SANStorage Area Network)
    RAC 中 ASM 实例名 与 节点的对应关系
    光纤通道(FC: Fibre Channel)
    Oracle expdp/impdp 使用示例
    RAC 中 ASM 实例名 与 节点的对应关系
    RAC 修改 DB 实例名 步骤
    InfiniBand 网络
    ORA09817: Write to audit file failed 解决方法
    RAC 安装patch 后启动实例 报错 ORA00439 feature not enabled Real Application Clusters 解决方法
  • 原文地址:https://www.cnblogs.com/myparamita/p/2352313.html
Copyright © 2011-2022 走看看