| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- /*******************************************************************************
- * Copyright(c) 2014 dongke All rights reserved. / Confidential
- * 类的信息:
- * 1.程序名称:INIUtility.cs
- * 2.功能描述:INI配置文件操作类
- * 编辑履历:
- * 作者 日期 版本 修改内容
- * 陈冰 2014/11/17 1.00 新建
- *******************************************************************************/
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- namespace Dongke.IBOSS.PRD.Basics.Library
- {
- /// <summary>
- /// INI配置文件操作类
- /// </summary>
- public class INIUtility
- {
- // 放置单例
- private static Dictionary<IniFile, INIUtility> _dic = new Dictionary<IniFile, INIUtility>();
- // ini文件路径
- private string _iniFilePath;
- /// <summary>
- /// 文件
- /// </summary>
- public enum IniFile
- {
- /// <summary>
- /// Config文件
- /// </summary>
- Config,
- }
- /// <summary>
- /// 构造
- /// </summary>
- private INIUtility()
- {
- }
- /// <summary>
- /// 单例
- /// </summary>
- /// <param name="iniFile">配置文件</param>
- /// <returns></returns>
- public static INIUtility Instance(IniFile iniFile)
- {
- if (_dic.Keys.Contains(iniFile))
- {
- return _dic[iniFile];
- }
- else
- {
- INIUtility ini = new INIUtility();
- ini._iniFilePath = System.AppDomain.CurrentDomain.BaseDirectory + iniFile.ToString() + ".ini";
- _dic.Add(iniFile, ini);
- return ini;
- }
- }
- #region API函数声明
- /// <summary>
- /// 返回0表示失败,非0为成功
- /// </summary>
- /// <param name="section"></param>
- /// <param name="key"></param>
- /// <param name="val"></param>
- /// <param name="filePath"></param>
- /// <returns></returns>
- [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, System.Text.StringBuilder retVal, int size, string filePath);
- #endregion
- #region 读Ini文件
- /// <summary>
- /// 读Ini文件
- /// </summary>
- /// <param name="section">section</param>
- /// <param name="key">key</param>
- /// <returns></returns>
- public string ReadIniData(string section, string key)
- {
- if (File.Exists(_iniFilePath))
- {
- // 每次从ini中读取多少字节
- StringBuilder temp = new StringBuilder(2048);
- // section=配置节,key=键名,temp=上面,path=路径
- GetPrivateProfileString(section, key, "", temp, 2048, _iniFilePath);
- return temp.ToString().Trim();
- }
- else
- {
- return string.Empty;
- }
- }
- public static string ReadIniData(string iniFile, string section, string key)
- {
- if (File.Exists(iniFile))
- {
- // 每次从ini中读取多少字节
- StringBuilder temp = new StringBuilder(2048);
- // section=配置节,key=键名,temp=上面,path=路径
- GetPrivateProfileString(section, key, "", temp, 2048, iniFile);
- return temp.ToString().Trim();
- }
- else
- {
- return string.Empty;
- }
- }
- #endregion
- #region 写Ini文件
- /// <summary>
- /// 写Ini文件
- /// </summary>
- /// <param name="Section"></param>
- /// <param name="Key"></param>
- /// <param name="Value"></param>
- /// <returns></returns>
- public bool WriteIniData(string Section, string Key, string Value)
- {
- //if (File.Exists(_iniFilePath))
- {
- long OpStation = WritePrivateProfileString(Section, Key, Value, _iniFilePath);
- if (OpStation == 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- //else
- //{
- // return false;
- //}
- }
- #endregion
- }
- }
|