ini檔是常見的設定檔格式,可參考:https://en.wikipedia.org/wiki/INI_file
讀取檔案很容易直覺想到StreamReader
但其實可以用win32 API有效率的讀寫.ini檔哦
- using System.Runtime.InteropServices;
- 在class中新增下列code
[DllImport( "kernel32", CharSet = CharSet.Unicode, SetLastError = true )]
private static extern bool WritePrivateProfileString( string sectionName, string keyName, string keyValue, string filePath );
[DllImport( "kernel32", CharSet = CharSet.Unicode, SetLastError = true )]
private static extern int GetPrivateProfileString( string sectionName, string keyName, string defaultReturnString, StringBuilder returnString, int returnStringLength, string filePath );
- 然後就可以使用了,引數與回傳值請參考MSDN
- WritePrivateProfileString:https://msdn.microsoft.com/zh-tw/library/windows/desktop/ms725501(v=vs.85).aspx
- GetPrivateProfileString:https://msdn.microsoft.com/en-us/library/windows/desktop/ms724353(v=vs.85).aspx
PS:WritePrivateProfileString中filePath若沒有寫全路徑似乎會指到C:\windows去
文章標籤
全站熱搜