久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

C# 操作INI文件

 實力決定地位 2015-04-02
#region 調用windowsAPI
//用來讀取INI 文件的內(nèi)容
[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
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
private static extern int GetPrivateProfileSection(string lpAppName,byte[] lpReturnedString, int nSize,string lpFileName);
//根據(jù)傳入?yún)?shù)的不同進行寫入或修改或刪除操作(返回值 Long,,非零表示成功,零表示失?。?
[DllImport("Kernel32.dll")]
public static extern long WritePrivateProfileString(string strAppName, string strKeyName, string strKeyValue, string strFileName);
//添加一個section內(nèi)容列表
[DllImport("Kernel32.dll")]
public static extern long WritePrivateProfileSection(string strAppName, string strkeyandvalue, string strFileName);
#endregion
調用這些函數(shù)的所用到的方法:
[html] view plaincopy
#region 供UI調用的方法
/// <summary>
/// 判斷該ini文件是否存在如果不存在新建一個該文件
/// </summary>
public void FileExists()
{
try
{
if (!File.Exists(this.filePath))
{
using (FileStream fs = File.Create(this.filePath))
{
fs.Close();
}
}
}
catch(Exception e)
{
}
}
/// <summary>
/// 返回該配置文件中所有Section名稱的集合
/// </summary>
/// <returns></returns>
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;
}
/// <summary>
/// 獲取指定節(jié)點的所有KEY的名稱
/// </summary>
/// <param name="sectionName"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 讀取指定節(jié)點下的指定key的value返回string
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <returns></returns>
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();
}
/// <summary>
/// 從指定的節(jié)點中獲取一個整數(shù)值( Long,,找到的key的值;如指定的key未找到,,就返回默認值,。如找到的數(shù)字不是一個合法的整數(shù),函數(shù)會返回其中合法的一部分,。如,,對于“xyz=55zz”這個條目,,函數(shù)返回55,。)
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <returns></returns>
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);
}
/// <summary>
/// 讀取指定節(jié)點下的所有key 和value
/// </summary>
/// <param name="section"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 往指定section的key中寫入value
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 修改指定section的key的值
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 刪除指定section的指定key
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 刪除指定section
/// </summary>
/// <param name="section"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 給一個節(jié)點寫入key和value列表
/// </summary>
/// <param name="section"></param>
/// <param name="ht"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 給一個節(jié)點寫入key 列表
/// </summary>
/// <param name="section"></param>
/// <param name="lstKeyValue"></param>
/// <returns></returns>
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

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點,。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,,謹防詐騙,。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報,。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多