当前位置:编程学习 > VB >>

VB.NET操作ini文件的方法

不清楚 .net 类库中是否有相关的类封装了GetPrivateProfileInt 等操作ini文件的 API 函数如果没有,是不是只能引用 API 了
追问:谢谢

答案:只能用API操作,别无他法。另外,建议使用Settings文件,注册表或者XML来取代INI,微软已经抛弃INI这种做法来实现配置文件的定义了。

 

下面我介绍一个读写INI文件的C#类并利用该类保存窗体的坐标,当程序再次运行的时候,窗体将显示在上次退出时的位置。

INIFILE类: 

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

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

using System.Text; 
namespace Ini

   publicclass IniFile
   { 

     publicstring path;   //INI文件名
     [DllImport("kernel32")]
     privatestaticexternlong WritePrivateProfileString(string section,string key,string val,string filePath);
     [DllImport("kernel32")] 

     privatestaticexternint GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath); 

     //声明读写INI文件的API函数   
        public IniFile(string INIPath)
       {
       path = INIPath;
     }

     //类的构造函数,传递INI文件名
        publicvoid IniWriteValue(string Section,string Key,string Value)
     {
        WritePrivateProfileString(Section,Key,Value,this.path);
     } 

     //写INI文件 

     publicstring IniReadValue(string Section,string Key)
       {
          StringBuilder temp = new StringBuilder(255);

       int i = GetPrivateProfileString(Section,Key,"",temp,255,this.path); 

       return temp.ToString(); 

     }

 

     //读取INI文件指定 

   } 

调用INIFILE类: 

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

增加如下代码:

using Ini;  //创建命名空间

//当窗体关闭时保存窗体坐标 

                  privatevoid Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
     {

       IniFile ini = new IniFile("C:\\test.ini"); 

       ini.IniWriteValue("LOC" ,"x" ,this.Location.X.ToString()  ); 

       ini.IniWriteValue("LOC " ,"y" ,this.Location.Y.ToString()  ); 

       //ToString方法将数字转换为字符串 

     } 

//当窗体启动时,读取INI文件的值并赋值给窗体 

     privatevoid Form1_Load(object sender, System.EventArgs e)
      {

       IniFile ini = new IniFile("C:\\test.ini"); 

       Point   p=new Point() ; 

       if ((ini.IniReadValue ("LOC" ,"x" )!="" ) && (ini.IniReadValue ("LOC" ,"y" )!="")) 

       //判断返回值,避免第一次运行时为空出错
       {

          p.X=int.Parse (ini.IniReadValue ("LOC" ,"x" ));

          p.Y =int.Parse (ini.IniReadValue ("LOC" ,"y" )); 

          // int.Parse将字符串转换为int

          this.Location =p; 

       }

     }  

 

可以通过调用kernel32.dll中的两个api:WritePrivateProfileString,GetPrivateProfileString来实现对ini 文件的读些。

具体实现的代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Text;


namespace iniprocess
{
 
     public class Form1 : System.Windows.Forms.Form
     {
          private System.Windows.Forms.TextBox textBox1;
          private System.Windows.Forms.Button button2;
          private System.Windows.Forms.Button button1;
  
          [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);

      public void IniWriteValue(string Section,string Key,string Value,string filepath)//对ini文件进行写操作的函数
      {
           WritePrivateProfileString(Section,Key,Value,filepath);
      }
        
      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();

      }

 }

 

转自: http://www.zzzyk.com/rainstar/archive/2006/05/01/389823.html

您可以直接通过转换工具自己把C#转换成VB.net的:

http://www.developerfusion.com/tools/convert/vb-to-csharp/

上一个:VB如何制作数码时钟,给代码
下一个:vb将BMP图转化为ICO代码

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,