Code: Operate *.INI File Class in C#

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);
            
            
forint 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(nullnull);
        }


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

    }

}

posted on   birdshome  阅读(1772)  评论(3编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 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——大语言模型本地部署的极速利器

导航

点击右上角即可分享
微信分享提示