一般情况下,我写的程序在界面装载时,读取配置文件中的各个项目,中间使用或修改后,在界面退出时,保存一下配置文件。

在还不是面对对象年代,基本上都会把读配置文件中的一个项目、写配置文件中的一个项目都写成一个子程序,这样调用方便。当需要多增加新的一个项目时,只要在界面装载、界面退出时,各增加一行。当时感觉已经是不错的处理方案了。

Program.cs 中“读写INI的API”代码

        // 声明INI文件的写操作函数 WritePrivateProfileString()
        [System.Runtime.InteropServices.DllImport("kernel32")]
        private static extern int WritePrivateProfileString(string section, string key, string val, string filePath);

        // 声明INI文件的读操作函数 GetPrivateProfileString()
        [System.Runtime.InteropServices.DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);

        public static bool bWriteINIValue(string sSection, string sKeyName, string sText, string sINIFileName)
        {
            int lRet = WritePrivateProfileString(sSection, sKeyName, sText, sINIFileName);
            if (lRet == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static string sGetINIValue(string sSection, string sKeyName, string sDefault, string sINIFileName)
        {
            int lRet;
            System.Text.StringBuilder sTemp = new StringBuilder(255);
            lRet = GetPrivateProfileString(sSection, sKeyName, sDefault, sTemp, 255, sINIFileName);
            if (lRet == 0)
            {
                return string.Empty;
            }
            else
            {
                return sTemp.ToString();
            }
        }
View Code

相关文章:

  • 2022-01-19
  • 2021-06-05
  • 2022-12-23
  • 2021-03-30
  • 2021-07-17
  • 2021-10-09
  • 2021-05-10
  • 2022-12-23
猜你喜欢
  • 2021-10-28
  • 2021-07-01
  • 2021-05-28
  • 2022-12-23
  • 2021-11-07
  • 2021-05-04
  • 2021-11-12
相关资源
相似解决方案