會員中心 | iThome online | iT邦部落格 | 小7聚樂部 | iThome download | apphome

載入中...

leonk0614

iT邦初學者
10級

C# Win32 API INI相關應用

整理了比較常用到的功能, 有需要的可以參考看看
I. WritePrivateProfileString
II. WritePrivateProfileSection
III. GetPrivateProfileString
IV. GetPrivateProfileSectionNames
V. GetPrivateProfileSection


發佈到:發佈到Facebook 發佈到噗浪 發佈到twitter
分享時間:2012-08-10 10:34:04
▼ ADVERTISEMENT ▼
分享內容
2
I. WritePrivateProfileString
 作用:指定Section, Key, Path,寫入Value
 呼叫方法:
[DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileString", SetLastError = true)]
private static extern uint WritePrivateProfileString(string section, string key, string val, string filePath);
 使用方法:
WritePrivateProfileString("SectionName", "KeyName", "Value", path);



II. WritePrivateProfileSection
 作用:改寫整個Section
 呼叫方法:
[DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileSection", SetLastError = true)]
static extern uint WritePrivateProfileSection(string section, string lpString, string filePath);
 特殊技巧:lpString給null可以刪除整個Section,給空值會清除Section內所有Key
 Section內容結構:範例“s1=10/0s2=20”,“ =”為key與value之分隔,“/0”為換行
 使用方法:
WritePrivateProfileSection("SectionName", " s1=10/0s2=20", path);
WritePrivateProfileSection("SectionName", null, path);
WritePrivateProfileSection("SectionName", " ", path);



III. GetPrivateProfileString
 作用:指定Section, Key, Path,讀取StringBuilder
 呼叫方法:
[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileString", SetLastError = true)]
private static extern uint GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
 使用方法:
StringBuilder sbValue = new StringBuilder();
string stDefault = "null";
GetPrivateProfileString("SectionName", "KeyName", stDefault, sbValue, 255, path);





IV. GetPrivateProfileSectionNames
 作用:指定Path, 讀取其中所有Section Name
 呼叫方法:
[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileSectionNames", SetLastError = true)]
private static extern uint GetPrivateProfileSectionNames(IntPtr retVal, uint size, string filePath);
 注意事項:輸出字串名稱分隔為"/0",最後一項為0
 使用方法:
public string[] GetSectionNames(string path)
{
uint MAX_BUFFER = 32767;
IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER);
uint bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, path);
return IntPtrToStringArray(pReturnedString, bytesReturned);
}

//指標資料轉字串陣列
private string[] IntPtrToStringArray(IntPtr pReturnedString, uint bytesReturned)
{
//use of Substring below removes terminating null for split
if (bytesReturned == 0)
{
Marshal.FreeCoTaskMem(pReturnedString);
return null;
}
string local = Marshal.PtrToStringAnsi(pReturnedString, (int)bytesReturned).ToString();
Marshal.FreeCoTaskMem(pReturnedString);
return local.Substring(0, local.Length - 1).Split('\0');
}







V. GetPrivateProfileSection
 作用:指定Section,路徑,取得Section內所有資料
 呼叫方法:
[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileSection", SetLastError = true)]
private static extern uint GetPrivateProfileSection(string section, IntPtr retVal, uint size, string filePath);
 使用方法:
public string[] GetSection(string section, string path)
{
uint MAX_BUFFER = 32767;
IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER);
uint bytesReturned = GetPrivateProfileSection(section, pReturnedString, MAX_BUFFER,
path);
return IntPtrToStringArray(pReturnedString, bytesReturned);
}

//指標資料轉字串陣列
private string[] IntPtrToStringArray(IntPtr pReturnedString, uint bytesReturned)
{
//use of Substring below removes terminating null for split
if (bytesReturned == 0)
{
Marshal.FreeCoTaskMem(pReturnedString);
return null;
}
string local = Marshal.PtrToStringAnsi(pReturnedString, (int)bytesReturned).ToString();
Marshal.FreeCoTaskMem(pReturnedString);
return local.Substring(0, local.Length - 1).Split('\0');
}

參考資料:http://www.pinvoke.net/ , 與其餘網路資料

C# Win32 API INI相關應用

目前沒有資料

回應

請填寫您的回應,長度限為1,000個字,回應不計點數,也不限使用次數



 

檢舉違規

違規事項:

*補充檢舉理由(可省略),字數不可超過100字

推薦

推薦理由:


*給分享者的鼓勵(可不填),字數不可超過100字

哈哈
毆飛
開心
抗議
落寞
睡覺
噴鼻血
No
失神
爆氣
疑惑
Orz
不耐煩
喜歡
臉紅
噎到
放手
打嗑睡
掰掰
放馬過來
敲碗
簽名
筆記
拍手
沙發
XD
無言
偷笑
翻桌
謝謝
灑花
抱抱
逃跑
炸死你
愛你
生日快樂
rock
嘆氣
下雨
衝刺
搖頭
拍照
打球
健身
駭客
射門
泡湯
踹共
唱歌
做菜

上傳圖片
▼ ADVERTISEMENT ▼

邦友收藏動態

最新收藏最多人推最多人收

新增收藏

收藏到iT邦 書籤小工具

「收藏到iT邦」讓你更方便收藏站外文章。可用下面其中一種方法安裝:

  • 拖拉上面的「收藏到iT邦」連結到瀏覽器的書籤列
  • 在連結上方按右鍵,選擇「加到我的最愛」

之後看到喜歡的站外文章,只要點一下「收藏到iT邦」,就會收藏起來囉

安裝「收藏快捷鍵」

安裝「收藏快捷鍵」,可以讓邦友直接透過Google工具列上的按扭,快速收藏站內、站外的網頁。

訂閱每日摘要

iT邦幫忙即日起提供「每日摘要」給尚未註冊的邦友,只要輸入您的E-mail,每日就可以收到最新的發問與分享