Ini文件的读写

	public class cls_ConfigRead
    {
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        /// <summary>
        /// 写入INI文件
        /// </summary>
        /// <param name="section">项目名称</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public static void WriteIni(string section, string key, string value, string iniPath)
        {
            WritePrivateProfileString(section, key, value, iniPath);
        }
        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="section">项目名称</param>
        /// <param name="key">键</param>
        /// <returns>返回值</returns>
        public static string ReaderIni(string section, string key, string iniPath)
        {
            try
            {
                StringBuilder temp = new StringBuilder(1024);
                int i = GetPrivateProfileString(section, key, "", temp, 500, iniPath);
                if (i == 0)
                {
                    return null;
                }
                return temp.ToString();
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 验证INI文件是否存在
        /// </summary>
        /// <returns>返回Bool</returns>
        public static bool ExsitFile(string iniPath)
        {
            return File.Exists(iniPath);
        }
    }
	//Ini文件的路径:
	string configPath = AppDomain.CurrentDomain.BaseDirectory + "Config.ini";
    if (!File.Exists(configPath))
    {
           MessageBox.Show("不存在配置文件:Config.ini");
          this.Close();
    }
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页