You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
ManageTools/ManageTools/INIFile.cs

163 lines
5.0 KiB

using System;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
/*
* INI文件操作类
*/
namespace ManageTools
{
public class INIFile
{
public delegate void EventHandler(object sender, EventArgs e);
public event EventHandler IniFileChanged;
public event EventHandler Initialization;
protected string IniFileName;
public INIFile(string FileName)
{
IniFileName = FileName;
}
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);
}
public void WriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.IniFileName);
}
public string ReadValue(string Section, string Key, string Default)
{
StringBuilder temp = new StringBuilder(500);
int i = GetPrivateProfileString(Section, Key, Default, temp, 500, this.IniFileName);
return temp.ToString();
}
private void NewDirectory(String path)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
// 文件尾增加
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();
}
// 文件尾增加
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
/*
section: 要写入的段落名
key: 要写入的键,如果该key存在则覆盖写入
val: key所对应的值
filePath: INI文件的完整路径和文件名
*/
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
/*
section:要读取的段落名
key: 要读取的键
defVal: 读取异常的情况下的缺省值
retVal: key所对应的值,如果该key不存在则返回空值
size: 值允许的大小
filePath: INI文件的完整路径和文件名
*/
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string defVal, System.Text.StringBuilder retVal, int size, string filePath);
}
}