***************************************************
更多精彩,歡迎進入:http://shop115376623.taobao.com
***************************************************
/*
========================================================================
C# 對Ini文件操作(C# ini文件操作類)
[IniFiles.cs]
= =蝶曉夢整理了好久,拿出來分享
注意,不要用相對路徑,要用絕對路徑,否則有幾個函數會把文件建立到“C:\Windows”目錄下
如果找不到建立的文件,很可能建立到“C:\Windows”目錄下了
代碼是蝶曉夢從網上收集的整理而成,改動并且增加了一些,感謝以前寫這些代碼的人
使用方法:添加到工程文件的引用現有項里面,
然后在想用的地方這樣用:
#region 讀寫配置文件
string AppPath= Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)+@"\";
string IniFile="MyIniFile.ini"; ??????????
IniFiles Ini = new IniFiles(AppPath+IniFile);//ini文件的絕對路徑
Ini.WriteValue("RunTime Control", "Running", "mystring");
#endregion
蝶曉夢說這個類能干很多事情,
比如增加一行注釋
Ini.AddNotes("This is a note");
比如寫入一個字符串的值
Ini.WriteValue("RunTime Control", "Running", "mystring");
比如寫入一個整型的值
Ini.WriteValue("RunTime Control", "Running", 0);
比如寫入一個布爾型的值
Ini.WriteValue("RunTime Control", "Running", true);
還能寫入datetime的和object的
讀的時候能讀字符串的,整型的,布爾的,datetime的,例如
bool running=Ini.ReadValue("RunTime Control", "Running", true);
其他功能自己可以增加或者發掘
寫了兩個事件,一個是當向Ini.FileName賦值時會觸發Ini文件改變的事件
還有一個是這個類實例化的時候會觸發一個
舉例子:
Ini.IniFileChanged+=new IniFiles.EventHandler(Ini_IniFileChanged);//注冊事件
然后事件觸發的內容寫在這里
void Ini_IniFileChanged(object sender, EventArgs e)
{
//寫代碼于此
}
蝶曉夢說,大家以后可以增加這個類的功能,使這個類更加強大
增加內容:蝶曉夢說,平時不要首先用addnotes和addtext,可能會因為文本編碼的問題出錯
========================================================================
*/
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
public class IniFiles
{
public delegate void EventHandler(object sender, EventArgs e);
public event EventHandler IniFileChanged;
public event EventHandler Initialization;
protected string IniFileName;
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文件的完整路徑和文件名
*/
/// <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);
}
/// <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(500);
int i = GetPrivateProfileString(Section, Key, Default, temp, 500, this.IniFileName);
return temp.ToString();
}
/// <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
}
/*
========================================================================
將上面的代碼保存成IniFiles.cs
然后添加到項目里
更新到2009-08-28
========================================================================
*/