123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- using System.Runtime.InteropServices;
- namespace LYFZ.WinAPI
- {
- /// <summary>
- /// Ini文件操作类
- /// </summary>
- public class IniFiles
- {
- public delegate void EventHandler(object sender, EventArgs e);
- public event EventHandler IniFileChanged;
- public event EventHandler Initialization;
- protected string IniFileName;
- /// <summary>
- /// 文件名
- /// </summary>
- public string FileName
- {
- get
- {
- return IniFileName;
- }
- set
- {
- if (value != IniFileName)
- {
- IniFileName = value;
- OnIniFileChanged(new EventArgs());
- }
- }
- }
- protected void OnIniFileChanged(EventArgs e)
- {
- if (IniFileChanged != null)
- IniFileChanged(null, e);
- }
- protected void OnInitialization(EventArgs e)
- {
- if (Initialization != null)
- Initialization(null, e);
- }
- [DllImport("kernel32")]
- private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
- /*
- section: 要写入的段落名
- key: 要写入的键,如果该key存在则覆盖写入
- val: key所对应的值
- filePath: INI文件的完整路径和文件名
- */
- [DllImport("kernel32")]
- private static extern int GetPrivateProfileString(string section, string key, string defVal, System.Text.StringBuilder retVal, int size, string filePath);
- /*
- section:要读取的段落名
- key: 要读取的键
- defVal: 读取异常的情况下的缺省值
- retVal: key所对应的值,如果该key不存在则返回空值
- size: 值允许的大小
- filePath: INI文件的完整路径和文件名
- */
- /* [DllImport("kernel32")]
- public static extern bool WritePrivateProfileString(byte[] section, byte[] key, byte[] val, string filePath);
- [DllImport("kernel32")]
- public static extern int GetPrivateProfileString(byte[] section, byte[] key, byte[] def, byte[] retVal, int size, string filePath);
- //与ini交互必须统一编码格式
- private static byte[] getBytes(string s, string encodingName)
- { return null == s ? null : Encoding.GetEncoding(encodingName).GetBytes(s); }
- public static string ReadString(string section, string key, string def, string fileName, string encodingName = "utf-8", int size = 40960)
- {
- byte[] buffer = new byte[size];
- int count = GetPrivateProfileString(getBytes(section, encodingName), getBytes(key, encodingName), getBytes(def, encodingName), buffer, size, fileName);
- return Encoding.GetEncoding(encodingName).GetString(buffer, 0, count).Trim();
- }
- public static bool WriteString(string section, string key, string value, string fileName, string encodingName = "utf-8")
- { return WritePrivateProfileString(getBytes(section, encodingName), getBytes(key, encodingName), getBytes(value, encodingName), fileName); }
- */
- /// <summary>
- /// 构造方法
- /// </summary>
- /// <param name="INIPath">文件路径</param>
- public IniFiles(string FileName)
- {
- IniFileName = FileName;
- }
- /// <summary>
- /// 写入INI文件
- /// </summary>
- /// <param name="Section">项目名称(如 [TypeName] )</param>
- /// <param name="Key">键</param>
- /// <param name="Value">值</param>
- public void WriteValue(string Section, string Key, string Value)
- {
- WritePrivateProfileString(Section, Key, Value, this.IniFileName);
- // WriteString(Section, Key, Value, this.IniFileName);
- }
- /// <summary>
- /// 删除指定段、键的值
- /// </summary>
- /// <param name="Section">要删除的值所在的段</param>
- /// <param name="Key">要删除的值所在的键</param>
- public void DeleteValue(string Section, string Key)
- {
- WritePrivateProfileString(Section, Key, null, this.IniFileName);
- // WriteString(Section, Key, null, this.IniFileName);
- }
- /// <summary>
- /// 删除指定段的值
- /// </summary>
- /// <param name="Section">要删除的值所在的段</param>
- public void DeleteValue(string Section)
- {
- WritePrivateProfileString(Section, null, null, this.IniFileName);
- // WriteString(Section, null, null, this.IniFileName);
- }
- /// <summary>
- /// 读出INI文件
- /// </summary>
- /// <param name="Section">项目名称(如 [TypeName] )</param>
- /// <param name="Key">键</param>
- public string ReadValue(string Section, string Key, string Default)
- {
- StringBuilder temp = new StringBuilder(5000);
- int i = GetPrivateProfileString(Section, Key, Default, temp, 5000, this.IniFileName);
- return temp.ToString();
- //return ReadString(Section, Key, Default, this.IniFileName);
- }
- /// <summary>
- /// 验证文件是否存在
- /// </summary>
- /// <returns>布尔值</returns>
- public bool ExistINIFile()
- {
- return File.Exists(IniFileName);
- }
- /// <summary>
- /// 创建文件夹
- /// </summary>
- /// <param name="path">路径</param>
- private void NewDirectory(String path)
- {
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- }
- /// <summary>
- /// 添加一行注释
- /// </summary>
- /// <param name="Notes">注释</param>
- public void AddNotes(string Notes)
- {
- string filename = IniFileName;
- string path;
- path = Directory.GetParent(filename).ToString();
- NewDirectory(path);
- FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
- StreamWriter sw = new StreamWriter(fs);
- sw.BaseStream.Seek(0, SeekOrigin.End);
- sw.WriteLine(@";" + Notes);
- sw.Flush();
- sw.Close();
- fs.Close();
- sw.Dispose();
- fs.Dispose();
- }
- /// <summary>
- /// 添加一行文本
- /// </summary>
- /// <param name="Text">文本</param>
- public void AddText(string Text)
- {
- string filename = IniFileName;
- string path;
- path = Directory.GetParent(filename).ToString();
- NewDirectory(path);
- FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
- StreamWriter sw = new StreamWriter(fs);
- sw.BaseStream.Seek(0, SeekOrigin.End);
- sw.WriteLine(Text);
- sw.Flush();
- sw.Close();
- fs.Close();
- sw.Dispose();
- fs.Dispose();
- }
- #region 重载
- public void WriteValue(string Section, string Key, int Value)
- {
- WriteValue(Section, Key, Value.ToString());
- }
- public void WriteValue(string Section, string Key, Boolean Value)
- {
- WriteValue(Section, Key, Value.ToString());
- }
- public void WriteValue(string Section, string Key, DateTime Value)
- {
- WriteValue(Section, Key, Value.ToString());
- }
- public void WriteValue(string Section, string Key, object Value)
- {
- WriteValue(Section, Key, Value.ToString());
- }
- public int ReadValue(string Section, string Key, int Default)
- {
- return Convert.ToInt32(ReadValue(Section, Key, Default.ToString()));
- }
- public bool ReadValue(string Section, string Key, bool Default)
- {
- return Convert.ToBoolean(ReadValue(Section, Key, Default.ToString()));
- }
- public DateTime ReadValue(string Section, string Key, DateTime Default)
- {
- return Convert.ToDateTime(ReadValue(Section, Key, Default.ToString()));
- }
- public string ReadValue(string Section, string Key)
- {
- return ReadValue(Section, Key, "");
- }
- #endregion
-
- }
- }
|