此文章列舉了C#調用系統API 對INI文件的添加、讀取、修改、刪除節點和值的方法和UI調用,ini文件必須是ANSI編碼,否則會出現亂碼現象。
方法類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Runtime.InteropServices;
using System.IO;
using System.Collections;
namespace ConfigureManager
{
public class ManagerConfigIni
{
private string filePath;
private bool flag;
public ManagerConfigIni(string filePath)
{
this.filePath = filePath;
}
#region 調用windowsAPI
[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);
//獲取ini文件所有的section
[DllImport("Kernel32.dll")]
private extern static int GetPrivateProfileSectionNamesA(byte[] buffer, int iLen, string fileName);
//獲取指定Section的key和value
[DllImport("Kernel32.dll")]
private static extern int GetPrivateProfileSection(string lpAppName,byte[] lpReturnedString, int nSize,string lpFileName);
//根據傳入參數的不同進行寫入或修改或刪除操作(返回值 Long,非零表示成功,零表示失敗)
[DllImport("Kernel32.dll")]
public static extern long WritePrivateProfileString(string strAppName, string strKeyName, string strKeyValue, string strFileName);
//添加一個section內容列表
[DllImport("Kernel32.dll")]
public static extern long WritePrivateProfileSection(string strAppName, string strkeyandvalue, string strFileName);
#endregion
#region 供UI調用的方法
//判斷該ini文件是否存在如果不存在新建一個該文件
public void FileExists()
{
try
{
if (!File.Exists(this.filePath))
{
using (FileStream fs = File.Create(this.filePath))
{
fs.Close();
}
}
}
catch(Exception e)
{
}
}
//返回該配置文件中所有Section名稱的集合
public ArrayList ReadSections()
{
byte[] buffer = new byte[65535];
int rel = GetPrivateProfileSectionNamesA(buffer, buffer.GetUpperBound(0), this.filePath);
int iCnt, iPos;
ArrayList arrayList = new ArrayList();
string tmp;
if (rel > 0)
{
iCnt = 0; iPos = 0;
for (iCnt = 0; iCnt < rel; iCnt++)
{
if (buffer[iCnt] == 0x00)
{
tmp = System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt - iPos).Trim();
iPos = iCnt + 1;
if (tmp != "")
arrayList.Add(tmp);
}
}
}
return arrayList;
}
// 獲取指定節點的所有KEY的名稱
public ArrayList ReadKeys(string sectionName)
{
byte[] buffer = new byte[5120];
int rel = GetPrivateProfileStringA(sectionName, null, "", buffer, buffer.GetUpperBound(0), this.filePath);
int iCnt, iPos;
ArrayList arrayList = new ArrayList();
string tmp;
if (rel > 0)
{
iCnt = 0; iPos = 0;
for (iCnt = 0; iCnt < rel; iCnt++)
{
if (buffer[iCnt] == 0x00)
{
tmp = System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt - iPos).Trim();
iPos = iCnt + 1;
if (tmp != "")
arrayList.Add(tmp);
}
}
}
return arrayList;
}
//讀取指定節點下的指定key的value返回string
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();
}
//從指定的節點中獲取一個整數值( Long,找到的key的值;如指定的key未找到,就返回默認值。如找到的數字不是一個合法的整數,函數會返回其中合法的一部分。如,對於“xyz=55zz”這個條目,函數返回55。)
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);
}
//讀取指定節點下的所有key 和value
public ArrayList GetIniSectionValue(string section)
{
byte[] buffer = new byte[5120];
int rel = GetPrivateProfileSection(section, buffer, buffer.GetUpperBound(0), this.filePath);
int iCnt, iPos;
ArrayList arrayList = new ArrayList();
string tmp;
if (rel > 0)
{
iCnt = 0; iPos = 0;
for (iCnt = 0; iCnt < rel; iCnt++)
{
if (buffer[iCnt] == 0x00)
{
tmp = System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt - iPos).Trim();
iPos = iCnt + 1;
if (tmp != "")
arrayList.Add(tmp);
}
}
}
return arrayList;
}
//往指定section的key中寫入value
public bool WriteIniKey(string section, string key, string value)
{
try
{
if (section.Trim().Length <= 0 || key.Trim().Length <= 0 || value.Trim().Length <= 0)
{
flag = false;
}
else
{
if (WritePrivateProfileString(section, key, value, this.filePath) == 0)
{
flag = false;
}
else
{
flag = true;
}
}
}
catch
{
flag = false;
}
return flag;
}
//修改指定section的key的值
public bool EditIniKey(string section, string key, string value)
{
try
{
if (section.Trim().Length <= 0 || key.Trim().Length <= 0 || value.Trim().Length <= 0)
{
flag = false;
}
else
{
if (WritePrivateProfileString(section, key, value, this.filePath) == 0)
{
flag = false;
}
else
{
flag = true;
}
}
}
catch
{
flag = false;
}
return flag;
}
//刪除指定section的指定key
public bool DeleteIniKey(string section, string key)
{
try
{
if (section.Trim().Length <= 0 || key.Trim().Length <= 0)
{
flag = false;
}
else
{
if (WritePrivateProfileString(section, key, null, this.filePath) == 0)
{
flag = false;
}
else
{
flag = true;
}
}
}
catch
{
flag = false;
}
return flag;
}
//刪除指定section
public bool DeleteIniSection(string section)
{
try
{
if (section.Trim().Length <= 0)
{
flag = false;
}
else
{
if (WritePrivateProfileString(section, null, null, this.filePath) == 0)
{
flag = false;
}
else
{
flag = true;
}
}
}
catch
{
flag = false;
}
return flag;
}
//給一個節點寫入key和value列表
public bool WriteIniSectionAndValue(string section, Hashtable ht)
{
string lpString = "";
try
{
if (section.Trim().Length <= 0 || ht.Count == 0)
{
flag = false;
}
else
{
foreach (DictionaryEntry de in ht)
{
lpString += de.Key+"="+de.Value;
lpString += "\r\n";
}
if (WritePrivateProfileSection(section, lpString, this.filePath) == 0)
{
flag = false;
}
else
{
flag = true;
}
}
}
catch
{
flag = false;
}
return flag;
}
//給一個節點寫入key 列表
public bool WriteIniSectionName(string section, List<string> lstKeyValue)
{
string lpString = "";
try
{
if (section.Trim().Length <= 0 || lstKeyValue.Count == 0)
{
flag = false;
}
else
{
for (int i = 0; i < lstKeyValue.Count; ++i)
{
lpString += lstKeyValue[i];
lpString += "\r\n";
}
if (WritePrivateProfileSection(section, lpString, this.filePath) == 0)
{
flag = false;
}
else
{
flag = true;
}
}
}
catch
{
flag = false;
}
return flag;
}
#endregion
}
}