1.Ini文件的操作主要調用了以下三個API函數:
//寫ini文件
[ DllImport ( "kernel32" ) ]
private static extern bool WritePrivateProfileString ( string section ,string key , string val , string filePath ) ;
//讀ini文件->字符
[ DllImport ( "kernel32" ) ]
private static extern int GetPrivateProfileString ( string section ,string key , string def , StringBuilder retVal ,int size , string filePath ) ;
//讀ini文件->數字
[ DllImport ( "kernel32" ) ]
private static extern int GetPrivateProfileInt ( string section ,string key , int def , string filePath ) ;
2.以下是利用上面的API函數寫成的通用的類.
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace Ini
...{
public class IniFile
...{
private string FFileName;
//寫ini文件
[ DllImport ( "kernel32" ) ]
private static extern bool WritePrivateProfileString ( string section ,string key , string val , string filePath ) ;
//讀ini文件->字符
[ DllImport ( "kernel32" ) ]
private static extern int GetPrivateProfileString ( string section ,string key , string def , StringBuilder retVal ,int size , string filePath ) ;
//讀ini文件->數字
[ DllImport ( "kernel32" ) ]
private static extern int GetPrivateProfileInt ( string section ,string key , int def , string filePath ) ;

/**//// <param name="filename">文件路徑加完整文件名</param>
public IniFile(string filename)
...{
FFileName = filename;
}

/**//// <param name="def">默認值</param>
public int ReadInt(string section,string key,int def)
...{
return GetPrivateProfileInt(section,key,def,FFileName);
}

/**//// <param name="def">默認值</param>
public string ReadString(string section,string key,string def)
...{
StringBuilder temp = new StringBuilder(1024);
GetPrivateProfileString(section,key,def,temp,1024,FFileName);
return temp.ToString();
}
public void WriteInt(string section,string key,int val)
...{
WritePrivateProfileString(section,key,val.ToString(),FFileName);
}
public void WriteString(string section,string key,string val)
...{
WritePrivateProfileString(section,key,val,FFileName);
}
public void DelKey(string section,string key)
...{
WritePrivateProfileString(section,key,null,FFileName);
}
public void DelSection(string section)
...{
WritePrivateProfileString(section,null,null,FFileName);
}
}
}3.以下是一個實例,該實例主要用於保存窗體位置到Initest.ini中.
using Ini;//導入引用
以下是代碼:
private void Form1_Load(object sender, System.EventArgs e)
...{
//窗體啟動時讀取INI文件
if(File.Exists(Application.StartupPath+"/Initest.ini"))
...{
IniFile ini = new IniFile(Application.StartupPath + "/Initest.ini");
Point p = new Point();
p.X=ini.ReadInt("Location","X",0 );
p.Y=ini.ReadInt("Location" ,"Y",0);
this.Location = p;
this.TextBoxX.Text = this.Location.X.ToString();
this.TextBoxY.Text = this.Location.Y.ToString();
}
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
...{
//窗體關閉時寫INI文件
IniFile ini = new IniFile(Application.StartupPath + "/Initest.ini");
ini.WriteInt("Location","X" ,this.Location.X);
ini.WriteInt("Location","Y",this.Location.Y);
ini.WriteString("TextBoxX","Text",this.TextBoxX.Text);
ini.WriteString("TextBoxY","Text",this.TextBoxY.Text);
}附:Initest.ini文件內容如下,該文件路徑與程序啟動路徑相同.
[Location]
X=129
Y=191
[TextBoxX]
Text=129
[TextBoxY]
Text=191
本文介绍了一种使用C#进行Ini文件读写的方法,并提供了一个通用的Ini文件操作类。该类封装了读取字符串、读取整数、写入字符串、写入整数等常用功能。此外,还通过实例演示了如何利用此类来保存窗体的位置。
1578

被折叠的 条评论
为什么被折叠?



