zoukankan      html  css  js  c++  java
  • C# 读取INI

    虽然微软早已经建议在WINDOWS中用注册表代替INI文件,但是在实际应用中,INI文件仍然有用武之地,尤其现在绿色软件的流行,越来越多的程序将自己的一些配置信息保存到了INI文件中。

      INI文件是文本文件,由若干节(section)组成,在每个带括号的标题下面,是若干个关键词(key)及其对应的值(Value):

      [Section]
      Key=Value

    VC中提供了API函数进行INI文件的读写操作,但是微软推出的C#编程语言中却没有相应的方法,下面我介绍一个读写INI文件的C#类并利用该类保存窗体的坐标,当程序再次运行的时候,窗体将显示在上次退出时的位置。

    INIFILE类:

    using System;
    using System.IO;
    using System.Runtime.InteropServices;

    因为我们需要调用API函数,所以必须创建System.Runtime.InteropServices命名空间以提供可用于访问 .NET 中的 COM 对象和本机 API 的类的集合。

    [c-sharp] view plaincopy
     
    1. using System.Text;  
    2.   
    3. namespace Ini  
    4. {  
    5.     public class IniFile  
    6.     {  
    7.         public string path;             //INI文件名  
    8.   
    9.         [DllImport("kernel32")]  
    10.         private static extern long WritePrivateProfileString(string section,string key,  
    11.                     string val,string filePath);  
    12.   
    13.         [DllImport("kernel32")]  
    14.         private static extern int GetPrivateProfileString(string section,string key,string def,  
    15.                     StringBuilder retVal,int size,string filePath);  
    16.   
    17.         //声明读写INI文件的API函数  
    18.         public IniFile(string INIPath)  
    19.         {  
    20.             path = INIPath;  
    21.         }  
    22.   
    23.         //类的构造函数,传递INI文件名  
    24.         publicvoid IniWriteValue(string Section,string Key,string Value)  
    25.         {  
    26.             WritePrivateProfileString(Section,Key,Value,this.path);  
    27.         }  
    28.   
    29.         //写INI文件  
    30.         publicstring IniReadValue(string Section,string Key)  
    31.         {  
    32.             StringBuilder temp = new StringBuilder(255);  
    33.             int i = GetPrivateProfileString(Section,Key,"",temp,255,this.path);  
    34.             return temp.ToString();  
    35.         }  
    36.   
    37.         //读取INI文件指定  
    38.     }  
    39. }  
     

    调用INIFILE类:

    新建一个标准的C# WINDOWS应用程序项目,在窗体中分别增加命名为sect、key、val的三个文本框。

    增加如下代码:

    using Ini;			//创建命名空间
    
    //当窗体关闭时保存窗体坐标
    [c-sharp] view plaincopy
     
    1. privatevoid Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)  
    2. {  
    3.     IniFile ini = new IniFile("C://test.ini");  
    4.     ini.IniWriteValue("LOC" ,"x" ,this.Location.X.ToString());  
    5.     ini.IniWriteValue("LOC " ,"y" ,this.Location.Y.ToString());  
    6.   
    7.     //ToString方法将数字转换为字符串  
    8. }  
    //当窗体启动时,读取INI文件的值并赋值给窗体
    [c-sharp] view plaincopy
     
    1. privatevoid Form1_Load(object sender, System.EventArgs e)  
    2. {  
    3.     IniFile ini = new IniFile("C://test.ini");  
    4.     Point p=new Point();  
    5.   
    6.     //判断返回值,避免第一次运行时为空出错  
    7.     if ((ini.IniReadValue ("LOC" ,"x" )!="" ) && (ini.IniReadValue ("LOC" ,"y" )!=""))  
    8.     {  
    9.         p.X=int.Parse (ini.IniReadValue ("LOC" ,"x" ));  
    10.         p.Y =int.Parse (ini.IniReadValue ("LOC" ,"y" ));  
    11.   
    12.         // int.Parse将字符串转换为int  
    13.         this.Location =p;  
    14.     }  
    15. }  
      
    ==============================================
    其他方法:
    DllImport("kernel32.dll")]
    public extern static int GetPrivateProfileString(string segName, string keyName, string sDefault, StringBuilder buffer, int nSize, string fileName);

    public extern static int GetPrivateProfileStringA(string segName, string keyName, string sDefault, byte[] buffer, int iLen, string fileName); // ANSI版本

    [DllImport("kernel32.dll")]
    public extern static int GetPrivateProfileSection(string segName, StringBuilder buffer, int nSize, string fileName);

    [DllImport("kernel32.dll")]
    public extern static int WritePrivateProfileSection(string segName, string sValue, string fileName);

    [DllImport("kernel32.dll")]
    public extern static int WritePrivateProfileString(string segName, string keyName, string sValue, string fileName);

    [DllImport("kernel32.dll")]
    public extern static int GetPrivateProfileSectionNamesA(byte[] buffer, int iLen, string fileName);

     
    [c-sharp] view plaincopy
     
    1. // 封装的方法中,最有价值的是获取所有Sections和所有的Keys,网上关于这个的代码大部分是错误的,这里给出一个正确的方法:  
    2. /// 返回该配置文件中所有Section名称的集合  
    3. public ArrayList ReadSections()  
    4. {  
    5.     byte[] buffer = new byte[65535];  
    6.     int rel = 0;// GetPrivateProfileSectionNamesA(buffer, buffer.GetUpperBound(0), _FileName);  
    7.     int iCnt, iPos;  
    8.     ArrayList arrayList = new ArrayList();  
    9.     string tmp;  
    10.     if (rel > 0)  
    11.     {  
    12.         iCnt = 0; iPos = 0;  
    13.         for (iCnt = 0; iCnt < rel; iCnt++)  
    14.         {  
    15.             if (buffer[iCnt] == 0x00)  
    16.             {  
    17.                 tmp = System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt - iPos).Trim();  
    18.                 iPos = iCnt + 1;  
    19.                 if (tmp != "")  
    20.                     arrayList.Add(tmp);  
    21.             }  
    22.         }  
    23.     }  
    24.     return arrayList;  
    25. }  
    26.   
    27. // 获取节点的所有KEY值  
    28.   
    29. public ArrayList ReadKeys(string sectionName)  
    30. {  
    31.   
    32.     byte[] buffer = new byte[5120];  
    33.     int rel = 0;// GetPrivateProfileStringA(sectionName, null, "", buffer, buffer.GetUpperBound(0), _FileName);  
    34.   
    35.     int iCnt, iPos;  
    36.     ArrayList arrayList = new ArrayList();  
    37.     string tmp;  
    38.     if (rel > 0)  
    39.     {  
    40.         iCnt = 0; iPos = 0;  
    41.         for (iCnt = 0; iCnt < rel; iCnt++)  
    42.         {  
    43.             if (buffer[iCnt] == 0x00)  
    44.             {  
    45.                 tmp = System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt - iPos).Trim();  
    46.                 iPos = iCnt + 1;  
    47.                 if (tmp != "")  
    48.                     arrayList.Add(tmp);  
    49.             }  
    50.         }  
    51.     }  
    52.     return arrayList;  
    53. }  
     
     
  • 相关阅读:
    Textarea自适应文字内容调整高度
    把页面某内容放入粘贴板中
    通过javamail发送电子邮件
    Jrebel+tomcat实现热部署
    Eclipse启动Tomcat时,45秒超时解决方式
    mybatis 多对多 处理
    单例模式
    Centos6安装mysql5.7
    maven手动导入jar包到本地仓库
    Jsp与servlet本质上的区别
  • 原文地址:https://www.cnblogs.com/ArRan/p/3903426.html
Copyright © 2011-2022 走看看