Code: Operate *.INI File Class in C#

简介:
None.gif using System;
None.gif using System.Text;
None.gif using System.Runtime.InteropServices;
None.gif
None.gif namespace Birdshome
ExpandedBlockStart.gif{
ExpandedSubBlockStart.gif     ///   <summary>
InBlock.gif    
///  Create a new INI file to store or load data
ExpandedSubBlockEnd.gif    
///   </summary>
InBlock.gif     public  class IniFile
ExpandedSubBlockStart.gif    {
InBlock.gif         private  string m_Path;
InBlock.gif 
InBlock.gif        # region Windows API Functions
InBlock.gif        [DllImport("kernel32")]
InBlock.gif         public  static  extern  long WritePrivateProfileString( string lpAppName,
InBlock.gif             string lpKeyName,  string lpReturnString,  string lpFilePath);
InBlock.gif        
InBlock.gif        [DllImport("kernel32")]
InBlock.gif         public  static  extern  int GetPrivateProfileString( string lpAppName,
InBlock.gif             string lpKeyName,  string lpDefault,  byte[] byBuffer,  int size,  string lpFilePath);
InBlock.gif 
InBlock.gif        [DllImport("kernel32")]
InBlock.gif         public  static  extern  int WritePrivateProfileSection( string lpAppName, 
InBlock.gif             string lpString,  string lpFileName);
InBlock.gif
InBlock.gif        [DllImport("kernel32")]
InBlock.gif         public  static  extern  int GetPrivateProfileSection( string lpAppName, 
InBlock.gif             string lpReturnString,  string lpFilePath);
InBlock.gif
InBlock.gif        [DllImport("kernel32")]
InBlock.gif         public  static  extern  int GetPrivateProfileInt( string lpAppName, 
InBlock.gif             string lpKeyName,  int iDefault,  string lpFilePath);
InBlock.gif         #endregion
InBlock.gif
ExpandedSubBlockStart.gif         ///   <summary>
InBlock.gif        
///  IniFile Constructor.
InBlock.gif        
///   </summary>
ExpandedSubBlockEnd.gif        
///   <PARAM name="IniPath"></PARAM>
InBlock.gif         public IniFile( string strIniPath)
ExpandedSubBlockStart.gif        {
InBlock.gif            m_Path = strIniPath;
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gif         ///   <summary>
InBlock.gif        
///  Write Data to the INI File
ExpandedSubBlockEnd.gif        
///   </summary>
InBlock.gif         public  long SetValue( string section,  string key,  string value)
ExpandedSubBlockStart.gif        {
InBlock.gif             return WritePrivateProfileString(section, key, value, m_Path);
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gif         ///   <summary>
InBlock.gif        
///  Read Data Value From the Ini File
ExpandedSubBlockEnd.gif        
///   </summary>
InBlock.gif         private  string [] GetValues( string section,  string key)
ExpandedSubBlockStart.gif        {
InBlock.gif             byte [] buff;
InBlock.gif             int BUFFER_SIZE = 0xff;
InBlock.gif             int iCount, iBufferSize, iMultiple = 0;
InBlock.gif             do
ExpandedSubBlockStart.gif            {
InBlock.gif                iMultiple ++;
InBlock.gif                iBufferSize = BUFFER_SIZE*iMultiple;
InBlock.gif                buff =  new  byte[iBufferSize];
InBlock.gif                iCount = GetPrivateProfileString(section, key, "", buff, iBufferSize, m_Path);
ExpandedSubBlockEnd.gif            }
InBlock.gif             while(iCount == iBufferSize-2);
InBlock.gif            
InBlock.gif             forint i=0 ; i < iCount ; ++i )
ExpandedSubBlockStart.gif            {
InBlock.gif                 if ( buff[i] == 0 && iCount != i+1 && buff[i+1] != 0 )
ExpandedSubBlockStart.gif                {
InBlock.gif                    buff[i] = ( int)'\n';
InBlock.gif                     continue;
ExpandedSubBlockEnd.gif                }
InBlock.gif                 if ( i > 0 && buff[i-1] == 0 && buff[i] == 0 )  break;
ExpandedSubBlockEnd.gif            }
InBlock.gif             string strResult = Encoding.Default.GetString(buff).Trim();
ExpandedSubBlockStart.gif             return strResult.Trim( new  char [] {'\0'}).Split( new  char [] {'\n'});
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif         public  string GetValue( string section,  string key)
ExpandedSubBlockStart.gif        {
InBlock.gif             if ( section ==  null || key ==  null )
ExpandedSubBlockStart.gif            {
InBlock.gif                 throw  new NullReferenceException("Section or Key");
ExpandedSubBlockEnd.gif            }
InBlock.gif             return GetValues(section, key)[0];
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif         public  string [] GetKeys( string section)
ExpandedSubBlockStart.gif        {
InBlock.gif             if ( section ==  null )
ExpandedSubBlockStart.gif            {
InBlock.gif                 throw  new NullReferenceException("Section");
ExpandedSubBlockEnd.gif            }
InBlock.gif             return GetValues(section,  null);
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif         public  string [] GetSections()
ExpandedSubBlockStart.gif        {
InBlock.gif             return GetValues( nullnull);
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif         public  long SetValueInt( string section,  string key,  int value)
ExpandedSubBlockStart.gif        {
InBlock.gif             return SetValue(section, key, value.ToString());
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif         public  int GetValueInt( string section,  string key)
ExpandedSubBlockStart.gif        {
InBlock.gif             return GetPrivateProfileInt(section, key, -1, m_Path);
ExpandedSubBlockEnd.gif        }
InBlock.gif 
InBlock.gif         public  int SetSection( string strSection,  string strKeyValue)
ExpandedSubBlockStart.gif        {
InBlock.gif             return WritePrivateProfileSection(strSection, strKeyValue, m_Path);
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif         public  int DeleteSection( string strSection)
ExpandedSubBlockStart.gif        {
InBlock.gif             return SetSection(strSection,  null);
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}

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

目录
相关文章
|
1月前
|
安全 Java 应用服务中间件
A configuration error occurred during startup.Please verify the preference field with the prompt: To
A configuration error occurred during startup.Please verify the preference field with the prompt: To
12 0
|
10月前
Newline required at end of file but not found.
Newline required at end of file but not found.
101 0
Newline required at end of file but not found.
Could not initialize English chunker/Could not load file from classpath: ‘/en-token.bin‘
Could not initialize English chunker/Could not load file from classpath: ‘/en-token.bin‘
83 0
|
Java 数据库 Android开发
Eclipse中出现Syntax error on token "class", invalid VariableDeclarator(关键字问题)
Eclipse中出现Syntax error on token "class", invalid VariableDeclarator(关键字问题)
112 0
Eclipse中出现Syntax error on token "class", invalid VariableDeclarator(关键字问题)
|
XML 数据格式
因为CircleImageView导致Binary XML file line #96: Error inflating class <unknown>
因为CircleImageView导致Binary XML file line #96: Error inflating class <unknown>
158 0
|
应用服务中间件 Android开发
a configuration error occurred during startup. place verify the preference field whth the prompt:TomcatJDK name:
a configuration error occurred during startup. place verify the preference field whth the prompt:TomcatJDK name:
106 0
a configuration error occurred during startup. place verify the preference field whth the prompt:TomcatJDK name:
How to add extension field to report
How to add extension field to report
106 0
|
JavaScript PHP 前端开发
PHPStorm File and Code Template
有时候我们想新建某类型文件的时候,默认出现一些基础代码,而不是空白的。 比如当新建一个 html 文件 基础代码是这样的: 同理,当新建php文件,我希望是这样:
1364 0
|
Java 应用服务中间件 Android开发