Ini文件是在Windows程序中使用较多的一种配置信息存放格式,并且微软提供了多个API来支持对其读写。不过比较遗憾的是在提倡xml结构存储数据的.NET Famework中,没有提供对Ini操作的封装。这对当我们要读取旧的Ini配置文件,或就是希望用Ini文件来存储少量的数据时,带来了一小点的郁闷。于是在此做了一个C#版的for .NET封装。
using System;
using System.Text;
using System.Runtime.InteropServices;
![](/Images/OutliningIndicators/None.gif)
namespace Birdshome
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Create a new INI file to store or load data
/// </summary>
public class IniFile
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private string m_Path;
# region Windows API Functions
[DllImport("kernel32")]
public static extern long WritePrivateProfileString(string lpAppName,
string lpKeyName, string lpReturnString, string lpFilePath);
[DllImport("kernel32")]
public static extern int GetPrivateProfileString(string lpAppName,
string lpKeyName, string lpDefault, byte[] byBuffer, int size, string lpFilePath);
[DllImport("kernel32")]
public static extern int WritePrivateProfileSection(string lpAppName,
string lpString, string lpFileName);
![](/Images/OutliningIndicators/InBlock.gif)
[DllImport("kernel32")]
public static extern int GetPrivateProfileSection(string lpAppName,
string lpReturnString, string lpFilePath);
![](/Images/OutliningIndicators/InBlock.gif)
[DllImport("kernel32")]
public static extern int GetPrivateProfileInt(string lpAppName,
string lpKeyName, int iDefault, string lpFilePath);
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// IniFile Constructor.
/// </summary>
/// <PARAM name="IniPath"></PARAM>
public IniFile(string strIniPath)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_Path = strIniPath;
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Write Data to the INI File
/// </summary>
public long SetValue(string section, string key, string value)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return WritePrivateProfileString(section, key, value, m_Path);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Read Data Value From the Ini File
/// </summary>
private string [] GetValues(string section, string key)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
byte [] buff;
int BUFFER_SIZE = 0xff;
int iCount, iBufferSize, iMultiple = 0;
do
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
iMultiple ++;
iBufferSize = BUFFER_SIZE*iMultiple;
buff = new byte[iBufferSize];
iCount = GetPrivateProfileString(section, key, "", buff, iBufferSize, m_Path);
}
while(iCount == iBufferSize-2);
for( int i=0 ; i < iCount ; ++i )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ( buff[i] == 0 && iCount != i+1 && buff[i+1] != 0 )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
buff[i] = (int)'\n';
continue;
}
if ( i > 0 && buff[i-1] == 0 && buff[i] == 0 ) break;
}
string strResult = Encoding.Default.GetString(buff).Trim();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
return strResult.Trim(new char []
{'\0'}).Split(new char []
{'\n'});
}
![](/Images/OutliningIndicators/InBlock.gif)
public string GetValue(string section, string key)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ( section == null || key == null )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new NullReferenceException("Section or Key");
}
return GetValues(section, key)[0];
}
![](/Images/OutliningIndicators/InBlock.gif)
public string [] GetKeys(string section)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ( section == null )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new NullReferenceException("Section");
}
return GetValues(section, null);
}
![](/Images/OutliningIndicators/InBlock.gif)
public string [] GetSections()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return GetValues(null, null);
}
![](/Images/OutliningIndicators/InBlock.gif)
public long SetValueInt(string section, string key, int value)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return SetValue(section, key, value.ToString());
}
![](/Images/OutliningIndicators/InBlock.gif)
public int GetValueInt(string section, string key)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return GetPrivateProfileInt(section, key, -1, m_Path);
}
public int SetSection(string strSection, string strKeyValue)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return WritePrivateProfileSection(strSection, strKeyValue, m_Path);
}
![](/Images/OutliningIndicators/InBlock.gif)
public int DeleteSection(string strSection)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return SetSection(strSection, null);
}
}
}