|
|
|
分享內容
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');
}
作用:指定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/ , 與其餘網路資料
|
|
|
哈哈
哭
怒
驚
毆飛
暈
開心
飛
抗議
落寞
睡覺
讚
忙
噴鼻血
No
汗
失神
爆氣
疑惑
Orz
冷
病
不耐煩
喜歡
臉紅
噎到
放手
打嗑睡
掰掰
放馬過來
敲碗
簽名
筆記
拍手
沙發
囧
XD
無言
偷笑
翻桌
謝謝
灑花
窮
瞎
倒
抱抱
逃跑
嗚
炸死你
愛你
遜
生日快樂
空
rock
嘆氣
下雨
衝刺
搖頭
吃
飽
醉
拍照
打球
健身
駭客
射門
泡湯
踹共
唱歌
做菜
▼ ADVERTISEMENT ▼
邦友收藏動態
- 賽門鐵克:臺灣是全球傀儡網路重災區 (a0953278022)
- 蒸發不了的價值 (s863323)
- Google I/O:Google釋出Android Studio整合開發環境 (licsist)
- 分享大小事-個人儲存除了硬體也要軟體來配,個人儲存限定文! (temo)
- 蒸發不了的價值 (gpmap)
- 【書摘】從領導臉書Big Data團隊經驗談起:資訊平臺與資料科學家的崛起 (prtamith)
- 預先知道公司會停電,伺服器和網路設備都要關機嗎 (prtamith)
- 徹底搜查- 封鎖USB 2012 最新13招 (zuyan)
- 徹底搜查- 封鎖USB 2012 最新13招 (vaisramana)
- 用群組原則輕鬆管理使用者電腦 (vaisramana)
相關問答
- 程式設計心法:29.程式設計師的特質
- IT Blog (23): 程式設計人手札
- PHP程式設計師的大腦 - 螢幕保護程式
- 程式設計心法:26.實作 Form 的繼承(下)
- flex可以用c#寫嗎?
- 程式設計心法:30.路未央
- 程式設計心法:25.實作 Form 的繼承(上)
- 程式設計心法:23.物件導向設計的步驟
- 易經網頁程式設計
- C# C#與VB程式碼互轉的網站
- 程式設計心法:22.物件?了了沒?
- 關於微軟正式道歉:真的有偷程式碼,將向噗浪說明這篇新聞
- 程式設計心法:27.小技巧--每月的最後一天
- 程式設計心法:21.Error Handling
- 請問所謂寫程式 要從哪裡入門 要先從哪一種語言先學 或是需要先學會哪一種應用程式 請有經驗的大大們指教 謝謝
- c # 可以開發 symbian os嗎?
- 程式設計心法:28.小技巧--Table V.S. Class
- (百萬獎額,等您來拿)~ AIO觸控生活應用程式設計競賽 募集創意點子
- 程式設計心法:24.練習曲
- 從無到有-實戰Android系統開發-程式實戰(2)

閱讀(782)

