[DllImport( "Kernel32.dll" )] |
private static extern int GetPrivateProfileString( string strAppName, string strKeyName, string strDefault,StringBuilder sbReturnString, int nSize, string strFileName); |
[DllImport( "Kernel32.dll" )] |
private extern static int GetPrivateProfileStringA( string strAppName, string strKeyName, string sDefault, byte [] buffer, int nSize, string strFileName); |
[DllImport( "Kernel32.dll" )] |
private static extern int GetPrivateProfileInt( string strAppName, string strKeyName, int nDefault, string strFileName); |
[DllImport( "Kernel32.dll" )] |
private extern static int GetPrivateProfileSectionNamesA( byte [] buffer, int iLen, string fileName); |
[System.Runtime.InteropServices.DllImport( "Kernel32.dll" )] |
private static extern int GetPrivateProfileSection( string lpAppName, byte [] lpReturnedString, int nSize, string lpFileName); |
[DllImport( "Kernel32.dll" )] |
public static extern long WritePrivateProfileString( string strAppName, string strKeyName, string strKeyValue, string strFileName); |
[DllImport( "Kernel32.dll" )] |
public static extern long WritePrivateProfileSection( string strAppName, string strkeyandvalue, string strFileName); |
/// 判斷該ini文件是否存在如果不存在新建一個該文件 |
if (!File.Exists( this .filePath)) |
using (FileStream fs = File.Create( this .filePath)) |
/// 返回該配置文件中所有Section名稱的集合 |
public ArrayList ReadSections() |
byte [] buffer = new byte [65535]; |
int rel = GetPrivateProfileSectionNamesA(buffer, buffer.GetUpperBound(0), this .filePath); |
ArrayList arrayList = new ArrayList(); |
for (iCnt = 0; iCnt < rel; iCnt++) |
if (buffer[iCnt] == 0x00) |
tmp = System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt - iPos).Trim(); |
/// <param name="sectionName"></param> |
public ArrayList ReadKeys( string sectionName) |
byte [] buffer = new byte [5120]; |
int rel = GetPrivateProfileStringA(sectionName, null , "" , buffer, buffer.GetUpperBound(0), this .filePath); |
ArrayList arrayList = new ArrayList(); |
for (iCnt = 0; iCnt < rel; iCnt++) |
if (buffer[iCnt] == 0x00) |
tmp = System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt - iPos).Trim(); |
/// 讀取指定節(jié)點下的指定key的value返回string |
/// <param name="section"></param> |
/// <param name="key"></param> |
public string GetIniKeyValueForStr( string section, string key) |
if (section.Trim().Length <= 0 || key.Trim().Length <= 0) return string .Empty; |
StringBuilder strTemp = new StringBuilder(256); |
GetPrivateProfileString(section, key, string .Empty, strTemp, 256, this .filePath); |
return strTemp.ToString().Trim(); |
/// 從指定的節(jié)點中獲取一個整數(shù)值( Long,,找到的key的值;如指定的key未找到,,就返回默認值,。如找到的數(shù)字不是一個合法的整數(shù),函數(shù)會返回其中合法的一部分,。如,,對于“xyz=55zz”這個條目,,函數(shù)返回55,。) |
/// <param name="section"></param> |
/// <param name="key"></param> |
public int GetIniKeyValueForInt( string section, string key) |
if (section.Trim().Length <= 0 || key.Trim().Length <= 0) return 0; |
return GetPrivateProfileInt(section, key, 0, this .filePath); |
/// 讀取指定節(jié)點下的所有key 和value |
/// <param name="section"></param> |
public ArrayList GetIniSectionValue( string section) |
byte [] buffer = new byte [5120]; |
int rel = GetPrivateProfileSection(section, buffer, buffer.GetUpperBound(0), this .filePath); |
ArrayList arrayList = new ArrayList(); |
for (iCnt = 0; iCnt < rel; iCnt++) |
if (buffer[iCnt] == 0x00) |
tmp = System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt - iPos).Trim(); |
/// 往指定section的key中寫入value |
/// <param name="section"></param> |
/// <param name="key"></param> |
/// <param name="value"></param> |
public bool WriteIniKey( string section, string key, string value) |
if (section.Trim().Length <= 0 || key.Trim().Length <= 0 || value.Trim().Length <= 0) |
if (WritePrivateProfileString(section, key, value, this .filePath) == 0) |
/// <param name="section"></param> |
/// <param name="key"></param> |
/// <param name="value"></param> |
public bool EditIniKey( string section, string key, string value) |
if (section.Trim().Length <= 0 || key.Trim().Length <= 0 || value.Trim().Length <= 0) |
if (WritePrivateProfileString(section, key, value, this .filePath) == 0) |
/// <param name="section"></param> |
/// <param name="key"></param> |
public bool DeleteIniKey( string section, string key) |
if (section.Trim().Length <= 0 || key.Trim().Length <= 0) |
if (WritePrivateProfileString(section, key, null , this .filePath) == 0) |
/// <param name="section"></param> |
public bool DeleteIniSection( string section) |
if (section.Trim().Length <= 0) |
if (WritePrivateProfileString(section, null , null , this .filePath) == 0) |
/// 給一個節(jié)點寫入key和value列表 |
/// <param name="section"></param> |
/// <param name="ht"></param> |
public bool WriteIniSectionAndValue( string section, Hashtable ht) |
if (section.Trim().Length <= 0 || ht.Count == 0) |
foreach (DictionaryEntry de in ht) |
lpString += de.Key+ "=" +de.Value; |
if (WritePrivateProfileSection(section, lpString, this .filePath) == 0) |
/// <param name="section"></param> |
/// <param name="lstKeyValue"></param> |
public bool WriteIniSectionName( string section, List< string > lstKeyValue) |
if (section.Trim().Length <= 0 || lstKeyValue.Count == 0) |
for ( int i = 0; i < lstKeyValue.Count; ++i) |
lpString += lstKeyValue[i]; |
if (WritePrivateProfileSection(section, lpString, this .filePath) == 0) |
|