代码如下:
- # 读写 Ini 文件
- using System.Runtime.InteropServices;
- public class IniFile
- {
- private string path;
- public IniFile(string iniPath)
- {
- this.path = iniPath;
- }
- [DllImport("kernel32")]
- private static extern int GetPrivateProfileString
- (string section, string key, string def, StringBuilder retVal, int size, string filePath);
- [DllImport("kernel32")]
- private static extern long WritePrivateProfileString
- (string section, string key, string val, string filePath);
- public string IniReadValue(string section, string key)
- {
- try
- {
- StringBuilder retVal = new StringBuilder(0xff);
- if (GetPrivateProfileString(section, key, "", retVal, 0xff, this.path) == 0)
- {
- return string.Empty;
- }
- return retVal.ToString();
- }
- catch (Exception exception)
- {
- return exception.ToString();
- }
- }
- public void IniWriteValue(string Section, string Key, string Value)
- {
- WritePrivateProfileString(Section, Key, Value, this.path);
- }
- }