123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- using System;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.IO;
- namespace Ant.Service.Utilities
- {
- /// <summary>
- /// INI文件读写类。
- /// </summary>
- public class INIClass
- {
- public string inipath;
- [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>
- /// 构造方法
- /// </summary>
- /// <param name="INIPath">文件路径</param>
- public INIClass(string INIPath)
- {
- inipath = INIPath;
- }
- /// <summary>
- /// 写入INI文件
- /// </summary>
- /// <param name="Section">项目名称(如 [TypeName] )</param>
- /// <param name="Key">键</param>
- /// <param name="Value">值</param>
- public void IniWriteValue(string Section, string Key, string Value)
- {
- WritePrivateProfileString(Section, Key, Value, this.inipath);
- }
- /// <summary>
- /// 读出INI文件
- /// </summary>
- /// <param name="Section">项目名称(如 [TypeName] )</param>
- /// <param name="Key">键</param>
- public string IniReadValue(string Section, string Key)
- {
- StringBuilder temp = new StringBuilder(500);
- int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
- return temp.ToString();
- }
- /// <summary>
- /// 验证文件是否存在
- /// </summary>
- /// <returns>布尔值</returns>
- public bool ExistINIFile()
- {
- return File.Exists(inipath);
- }
- /// <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 = inipath;
- 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 = inipath;
- 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();
- }
- }
- }
|