iT邦幫忙

2

C# Win32 API INI相關應用

  • 分享至 

  • xImage
  •  

整理了比較常用到的功能, 有需要的可以參考看看
I. WritePrivateProfileString
II. WritePrivateProfileSection
III. GetPrivateProfileString
IV. GetPrivateProfileSectionNames
V. GetPrivateProfileSection
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');
}


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言