• 关于C#操作INI文件的总结


    关于C#操作INI文件的总结

     
     

           INI文件其实是一种具有特定结构的文本文件,它的构成分为三部分,结构如下:

    [Section1]
    key 1 = value2
    key 1 = value2
    ……
    [Section2]
    key 1 = value1
    key 2 = value2
    ……

    文件由若干个段落(section)组成,每个段落又分成若干个键(key)和值(value)。Windows系统自带的Win32的API函数GetPrivateProfileString()和WritePrivateProfileString()分别实现了对INI文件的读写操作,他们位于kernel32.dll下。

    但是令人遗憾的是C#所使用的.NET框架下的公共类库并没有提供直接操作INI文件的类,所以唯一比较理想的方法就是调用API函数。

    然后,.Net框架下的类库是基于托管代码的,而API函数是基于非托管代码的,(在运行库的控制下执行的代码称作托管代码。相反,在运行库之外运行的代码称作非托管代码。)如何实现托管代码与非托管代码之间的操作呢?.Net框架的System.Runtime.InteropServices命名空间下提供各种各样支持COM interop及平台调用服务的成员,其中最重要的属性之一DllImportAttribute可以用来定义用于访问非托管API的平台调用方法,它提供了对从非托管DLL导出的函数进行调用所必需的信息。下面就来看一下如何实现C#与API函数的互操作。

    读操作:

    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section, string key, string defVal, StringBuilder retVal, int size, string filePath); 
    section:要读取的段落名
    key: 要读取的键
    defVal: 读取异常的情况下的缺省值
    retVal: key所对应的值,如果该key不存在则返回空值
    size: 值允许的大小
    filePath: INI文件的完整路径和文件名

    写操作:

    [DllImport("kernel32")] 
    private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); 
    section: 要写入的段落名
    key: 要写入的键,如果该key存在则覆盖写入
    val: key所对应的值
    filePath: INI文件的完整路径和文件名

           这样,在就可以使用对他们的调用,用常规的方式定义一个名为IniFile类:

     1using System;
     2using System.Runtime.InteropServices; 
     3using System.Text; 
     4
     5namespace IPVOD.Hotel.Remoting
     6{
     7    /// <summary>
     8    /// INI文件的操作类
     9    /// </summary>
    10    public class IniFile
    11    {
    12        public string Path;
    13
    14        public IniFile(string path)
    15        {
    16            this.Path = path;
    17        }
    18        
    19        声明读写INI文件的API函数
    29
    30        /// <summary>
    31        /// 写INI文件
    32        /// </summary>
    33        /// <param name="section">段落</param>
    34        /// <param name="key">键</param>
    35        /// <param name="iValue">值</param>
    36        public void IniWriteValue(string section, string key, string iValue) 
    37        {
    38            WritePrivateProfileString(section, key, iValue, this.Path);
    39        }
    40
    41        /// <summary>
    42        /// 读取INI文件
    43        /// </summary>
    44        /// <param name="section">段落</param>
    45        /// <param name="key">键</param>
    46        /// <returns>返回的键值</returns>
    47        public string IniReadValue(string section, string key) 
    48        { 
    49            StringBuilder temp = new StringBuilder(255); 
    50
    51            int i = GetPrivateProfileString(section, key, "", temp, 255, this.Path); 
    52            return temp.ToString();
    53        }
    54
    55        /// <summary>
    56        /// 读取INI文件
    57        /// </summary>
    58        /// <param name="Section">段,格式[]</param>
    59        /// <param name="Key">键</param>
    60        /// <returns>返回byte类型的section组或键值组</returns>
    61        public byte[] IniReadValues(string section, string key)
    62        {
    63            byte[] temp = new byte[255];
    64
    65            int i = GetPrivateProfileString(section, key, "", temp, 255, this.Path);
    66            return temp;
    67        }
    68    }
    69}
    70

    注意:我增加了DLL导出的函数GetPrivateProfileString的重载,说明如下:

    [DllImport("kernel32")] 
    private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath);
    section:要读取的段落名
    key: 要读取的键
    defVal: 读取异常的情况下的缺省值
    retVal: 此参数类型不是string,而是Byte[]用于返回byte类型的section组或键值组。
    size: 值允许的大小
    filePath: INI文件的完整路径和文件名


    下面看一下具体实例化IniFile类的操作:

    //path为ini文件的物理路径

    IniFile ini = new IniFile(path);

    //读取ini文件的所有段落名

    byte[] allSection = ini.IniReadValues(null, null);

    通过如下方式转换byte[]类型为string[]数组类型

    string[] sectionList;

    ASCIIEncoding ascii = new ASCIIEncoding();

    //获取自定义设置section中的所有key,byte[]类型

    sectionByte = ini.IniReadValues("personal", null);

    //编码所有key的string类型

    sections = ascii.GetString(sectionByte);

    //获取key的数组

    sectionList = sections.Split(new char[1]{''});

    //读取ini文件personal段落的所有键名,返回byte[]类型

    byte[] sectionByte = ini.IniReadValues("personal", null);

    //读取ini文件evideo段落的MODEL键值

    model = ini.IniReadValue("evideo", "MODEL");

    //将值eth0写入ini文件evideo段落的DEVICE键

    ini.IniWriteValue("evideo", "DEVICE", "eth0");

    即:

    [evideo]

    DEVICE = eth0

    //删除ini文件下personal段落下的所有键

    ini.IniWriteValue("personal", null, null);

    //删除ini文件下所有段落

    ini.IniWriteValue(null, null, null);

    这样就实现了C#对ini文件包括段落section,键key,键值value的基本上所有操作,当然这只是简单的举例,不是详细的实现,欢迎随时提出任何疑问和建议。

     
    原文引入:http://www.cnblogs.com/gaohades/archive/2006/01/24/322751.html
     
     
    ------------------------------------------------------------
     

    c# ini文件  

    2010-03-01 22:40:17|  分类: c# 学习|字号 订阅

     
     

    .ini为配置文件。

    在winform下使用

    Using system.IO;

    //函数作用:向INI文件中写信息
            [DllImport("kernel")]
            public static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
            //函数作用:向INI文件中写信息


            [DllImport("kernel")]
            public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
            //函数作用:从私有文件中获取字符串(读Ini文件)
            //section:欲在其中查找条目的小节名称
            //key:欲获取的项名或条目名
            //def:指定的条目没有找到时返回的默认值。可设为空("") 
            //retVal:指定一个字串缓冲区,长度至少为size 
            //size:缓冲区大小
            //filePath:INI文件的完整路径


            /// <summary>
            /// 写ini文件函数
            /// </summary>
            /// <param name="Section">Section</param>
            /// <param name="Key">关键字</param>
            /// <param name="Value">要设置的值</param>
            /// <param name="filepath">ini文件路径</param>
            public void IniWriteValue(string Section, string Key, string Value, string filepath)//对ini文件进行写操作的函数
            {
                WritePrivateProfileString(Section, Key, Value, filepath);
            }
            /// <summary>
            /// 读ini文件函数
            /// </summary>
            /// <param name="Section">Section</param>
            /// <param name="Key">关键字</param>
            /// <param name="filepath">文件路径</param>
            /// <returns>返回string</returns>
            public string IniReadValue(string Section, string Key, string filepath)//对ini文件进行读操作的函数
            {
                StringBuilder temp = new StringBuilder(255);
                int i = GetPrivateProfileString(Section, Key, "", temp, 255, filepath);
                return temp.ToString();
            }

     
     
     
     
  • 相关阅读:
    多线程的几种实现方法详解
    Java线程编程中isAlive()和join()的使用详解
    MyEclipse在不同编辑面间快速切换
    MyEclipse中设置代码块快捷键
    MyEclipse设置文件编码
    Oracle安装后遇到错误:The Network Adapter could not establish the connection
    Java中的Runtime类
    Java中接口的特点
    Java中三种常见的注释(注解) Annotation
    Java中的泛型
  • 原文地址:https://www.cnblogs.com/meimao5211/p/3333937.html
Copyright © 2020-2023  润新知