Ini文件是在Windows程序中使用较多的一种配置信息存放格式,并且微软提供了多个API来支持对其读写。不过比较遗憾的是在提倡xml结构存储数据的.NET Famework中,没有提供对Ini操作的封装。这对当我们要读取旧的Ini配置文件,或就是希望用Ini文件来存储少量的数据时,带来了一小点的郁闷。于是在此做了一个C#版的for .NET封装。
using System;
using System.Text;
using System.Runtime.InteropServices;

namespace Birdshome


{

/**//// <summary>
/// Create a new INI file to store or load data
/// </summary>
public class IniFile

{
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);

[DllImport("kernel32")]
public static extern int GetPrivateProfileSection(string lpAppName,
string lpReturnString, string lpFilePath);

[DllImport("kernel32")]
public static extern int GetPrivateProfileInt(string lpAppName,
string lpKeyName, int iDefault, string lpFilePath);
#endregion


/**//// <summary>
/// IniFile Constructor.
/// </summary>
/// <PARAM name="IniPath"></PARAM>
public IniFile(string strIniPath)

{
m_Path = strIniPath;
}


/**//// <summary>
/// Write Data to the INI File
/// </summary>
public long SetValue(string section, string key, string value)

{
return WritePrivateProfileString(section, key, value, m_Path);
}


/**//// <summary>
/// Read Data Value From the Ini File
/// </summary>
private string [] GetValues(string section, string key)

{
byte [] buff;
int BUFFER_SIZE = 0xff;
int iCount, iBufferSize, iMultiple = 0;
do

{
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 )

{
if ( buff[i] == 0 && iCount != i+1 && buff[i+1] != 0 )

{
buff[i] = (int)'\n';
continue;
}
if ( i > 0 && buff[i-1] == 0 && buff[i] == 0 ) break;
}
string strResult = Encoding.Default.GetString(buff).Trim();

return strResult.Trim(new char []
{'\0'}).Split(new char []
{'\n'});
}

public string GetValue(string section, string key)

{
if ( section == null || key == null )

{
throw new NullReferenceException("Section or Key");
}
return GetValues(section, key)[0];
}

public string [] GetKeys(string section)

{
if ( section == null )

{
throw new NullReferenceException("Section");
}
return GetValues(section, null);
}

public string [] GetSections()

{
return GetValues(null, null);
}

public long SetValueInt(string section, string key, int value)

{
return SetValue(section, key, value.ToString());
}

public int GetValueInt(string section, string key)

{
return GetPrivateProfileInt(section, key, -1, m_Path);
}
public int SetSection(string strSection, string strKeyValue)

{
return WritePrivateProfileSection(strSection, strKeyValue, m_Path);
}

public int DeleteSection(string strSection)

{
return SetSection(strSection, null);
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器