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.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Create a new INI file to store or load data
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class IniFile
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.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.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// IniFile Constructor.
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <PARAM name="IniPath"></PARAM>

InBlock.gif        public IniFile(string strIniPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            m_Path 
= strIniPath;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.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.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return WritePrivateProfileString(section, key, value, m_Path);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Read Data Value From the Ini File
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private string [] GetValues(string section, string key)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
byte [] buff;
InBlock.gif            
int BUFFER_SIZE = 0xff;
InBlock.gif            
int iCount, iBufferSize, iMultiple = 0;
InBlock.gif            
do
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.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.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ( buff[i] == 0 && iCount != i+1 && buff[i+1!= 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.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.gifContractedSubBlock.gif            
return strResult.Trim(new char [] dot.gif{'\0'}).Split(new char [] dot.gif{'\n'});
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string GetValue(string section, string key)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( section == null || key == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.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.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( section == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.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.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return GetValues(nullnull);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public long SetValueInt(string section, string key, int value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return SetValue(section, key, value.ToString());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int GetValueInt(string section, string key)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return GetPrivateProfileInt(section, key, -1, m_Path);
ExpandedSubBlockEnd.gif        }

InBlock.gif 
InBlock.gif        
public int SetSection(string strSection, string strKeyValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return WritePrivateProfileSection(strSection, strKeyValue, m_Path);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int DeleteSection(string strSection)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return SetSection(strSection, null);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值