• C#操作INI


    /// <summary>
      /// 操作INI文件类 
      /// </summary>
      public class IniFile
      {
          const int DATA_SIZE = 1024;
    
          private string _path; //INI档案名 
          public string IniPath { get { return _path; } set { _path = value; } }
    
          [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
          public struct STRINGBUFFER
          {
              [MarshalAs(UnmanagedType.ByValTStr, SizeConst = DATA_SIZE)]
              public string szText;
          }
    
          //读写INI文件的API函数 
          [DllImport("kernel32", CharSet = CharSet.Auto)]
          private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
    
          [DllImport("kernel32", CharSet = CharSet.Auto)]
          private static extern int GetPrivateProfileString(string section, string key, string def, out STRINGBUFFER retVal, int size, string filePath);
    
          //类的构造函数,传递INI档案名 
          public IniFile(string INIPath)
          {
              _path = INIPath;
              if (!File.Exists(_path)) CreateIniFile();
          }
        

    public IniFile()
    {
    _path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "/config.ini");
    if (!File.Exists(_path)) CreateIniFile();
    }

    //写INI文件 
          public void IniWriteValue(string Section, string Key, string Value)
          {
              WritePrivateProfileString(Section, Key, Value, this._path);
          }
    
          //读取INI文件指定项目的数据 
          public string IniReadValue(string Section, string Key)
          {
              int i;
              STRINGBUFFER RetVal;
              i = GetPrivateProfileString(Section, Key, null, out RetVal, DATA_SIZE, this._path);
              string temp = RetVal.szText;
              return temp.Trim();
          }
    
          //读取INI文件指定项目的数据 
          public string IniReadValue(string Section, string Key, string defaultValue)
          {
              int i;
              STRINGBUFFER RetVal;
              i = GetPrivateProfileString(Section, Key, null, out RetVal, DATA_SIZE, this._path);
              string temp = RetVal.szText;
              return temp.Trim() == "" ? defaultValue : temp.Trim();
          }
    
          /// <summary>
          /// 创建INI文件
          /// </summary>
          public void CreateIniFile()
          {

    var dir = Path.GetDirectoryName(_path);
    if (!Directory.Exists(dir))
    Directory.CreateDirectory(dir);

    
              StreamWriter w = File.CreateText(_path);
              w.Write("");
              w.Flush();
              w.Close();
          }
      }
    慎于行,敏于思!GGGGGG
  • 相关阅读:
    状态管理cookie 案例
    JavaScript对象(document对象 图片轮播)
    JavaScript对象(窗口对象 定时器对象 )
    JavaScript对象(正则表达式,Date对象,function对象 arguments对象)
    CSS概述<选择器总结>
    HTML表单
    Web表格
    JDBC项目实践
    JDBC获取表的主键
    JDBC中DAO事务函数模版
  • 原文地址:https://www.cnblogs.com/GarsonZhang/p/4062749.html
Copyright © 2020-2023  润新知