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

本文转自博客园鸟食轩的博客,原文链接:http://www.cnblogs.com/birdshome/,如需转载请自行联系原博主。

目录
相关文章
|
10月前
|
监控 C#
55.c#:file类
55.c#:file类
140 1
|
C#
C# File、FileInfo、Directory、DirectoryInfo
本文主要介绍文件类、文件信息类、目录类、目录信息类的常用属性和方法
74 0
|
4月前
|
C# 开发者
C# 一分钟浅谈:Code Contracts 与契约编程
【10月更文挑战第26天】本文介绍了 C# 中的 Code Contracts,这是一个强大的工具,用于通过契约编程增强代码的健壮性和可维护性。文章从基本概念入手,详细讲解了前置条件、后置条件和对象不变量的使用方法,并通过具体代码示例进行了说明。同时,文章还探讨了常见的问题和易错点,如忘记启用静态检查、过度依赖契约和性能影响,并提供了相应的解决建议。希望读者能通过本文更好地理解和应用 Code Contracts。
71 3
|
2月前
|
C# 开发工具 C++
code runner 运行C#项目
本文介绍了如何修改Code Runner设置使 Visual Studio Code (VS Code) 能直接运行完整的 C# 项目。传统方式依赖 cscript 工具,仅支持 .csx 文件,功能受限且已停止维护。新配置使用 `dotnet run` 命令,结合一系列炫酷的cmd指令,将指令定位到具体的csproj文件上进行运行。
131 38
|
7月前
|
存储 API C#
【Azure Developer】解决Azure Key Vault管理Storage的示例代码在中国区Azure遇见的各种认证/授权问题 - C# Example Code
【Azure Developer】解决Azure Key Vault管理Storage的示例代码在中国区Azure遇见的各种认证/授权问题 - C# Example Code
C# (File方法)对文件的操作,字节写入和读取
C# (File方法)对文件的操作,字节写入和读取
C#视频---文件管理file类
C#视频---文件管理file类
96 0
C#编程-110:文件操作File静态类
C#编程-110:文件操作File静态类
153 0
C#编程-110:文件操作File静态类
|
C#
30天C#基础巩固------集合,File(文件操作 ),Encoding处理字符集
30天C#基础巩固------集合,File(文件操作 ),Encoding处理字符集
144 0
30天C#基础巩固------集合,File(文件操作 ),Encoding处理字符集