zoukankan      html  css  js  c++  java
  • 【代码】ini 文件读取工具类

    using System;
    using System.Runtime.InteropServices;
    using System.Text;
    namespace hrattendance.Common { /// <summary> /// INIFile 的摘要说明。 /// </summary> public class INIFile { public string path; public INIFile(string INIPath) { path = INIPath; } #region 声明 [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath); #endregion
    #region 写INI /// <summary> /// 写INI文件 /// </summary> /// <param name="Section"></param> /// <param name="Key"></param> /// <param name="Value"></param> public void IniWriteValue(string Section, string Key, string Value) { WritePrivateProfileString(Section, Key, Value, this.path); } #endregion #region 删除ini配置 /// <summary> /// 删除ini文件下所有段落 /// </summary> public void ClearAllSection() { IniWriteValue(null, null, null); } /// <summary> /// 删除ini文件下personal段落下的所有键 /// </summary> /// <param name="Section"></param> public void ClearSection(string Section) { IniWriteValue(Section, null, null); } #endregion #region 读取INI /// <summary> /// 读取INI文件 /// </summary> /// <param name="Section"></param> /// <param name="Key"></param> /// <returns></returns> public string IniReadValue(string Section, string Key) { StringBuilder temp = new StringBuilder(1000); int i = GetPrivateProfileString(Section, Key, "", temp, 1000, this.path); return temp.ToString(); } public byte[] IniReadValues(string section, string key) { byte[] temp = new byte[1000]; int i = GetPrivateProfileString(section, key, "", temp, 1000, this.path); return temp; } /// <summary> /// 读取ini文件的所有段落名 /// </summary> public string[] IniReadValues() { byte[] allSection = IniReadValues(null, null); return ByteToString(allSection); } /// <summary> /// 转换byte[]类型为string[]数组类型 /// </summary> /// <param name="sectionByte"></param> /// <returns></returns> private string[] ByteToString(byte[] sectionByte) { ASCIIEncoding ascii = new ASCIIEncoding(); //编码所有key的string类型 string sections = ascii.GetString(sectionByte); //获取key的数组 string[] sectionList = sections.Split(new char[1] { '' }); return sectionList; } /// <summary> /// 读取ini文件的某段落下所有键名 /// </summary> public string[] IniReadValues(string Section) { byte[] sectionByte = IniReadValues(Section, null); return ByteToString(sectionByte); } #endregion } }

    PS:

    INI就是扩展名为"INI"的文件,其实他本身是个文本文件,可以用记事本打工,主要存放的是用户所做的选择或系统的各种参数.
    INI文件其实并不是普通的文本文件.它有自己的结构.由若干段落(SECTION)组成,在每个带括号的标题下面,是若干个以单个单词开头的关键字(KEYWORD)和一个等号,等号右边就是关键字的值(VALUE).例如:
    [Section1]
        KeyWord1 = Value1
        KeyWord2 = Value2
        ...
    [Section2]
        KeyWord3 = Value3
        KeyWord4 = Value4

  • 相关阅读:
    window.open的火狐、谷歌兼容写法
    一个分数怎样约分?想知道的速度了。。。
    这是第二道题内容要求写一个银行的ATM系统 这个浪费了好长时间 ,遇到了许多问题,不过都解决了,上程序
    两个有理数相加(要求输入时以分数形式,输出时也以分数形式)
    linux centos 7.5下 源码编译安装 lua环境
    SecureCRT 6.7 vim高亮
    C#第一章笔记
    HTML5考试错题
    第九章博客
    第八章博客
  • 原文地址:https://www.cnblogs.com/mushishi/p/3467408.html
Copyright © 2011-2022 走看看