110,566
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.IO;
using System.Windows.Forms;
using System.Text;
using System.Text.RegularExpressions;
public class InstallIni {
///<summary>设置Ini文件路径</summary>
public String Files {
get { return AttrFiles; }
set {
AttrFiles = value;
if ( File.Exists(AttrFiles) ) {
Open();
} else {
FilesText = "";
}
}
}
public String AttrFiles = "";
///<summary>Ini文件所有内容</summary>
public String FilesText {
get { return AttrFilesText; }
set { AttrFilesText = value; }
}
public String AttrFilesText = "";
///<summary>创建与Exe文件名相同的Ini文件</summary>
public InstallIni() {
String HaFile = Application.ExecutablePath;
HaFile = new Regex(@"\\[^\\]*$").Match(HaFile).Value;
HaFile = new Regex(@"(^\\)|(\.(.*?)$)").Replace(HaFile, "");
HaFile = HaFile + ".Ini";
Files = HaFile;
}
///<summary>创建指定路径文件名Ini文件</summary>
public InstallIni(String DataFile) {
Files = DataFile;
}
///<summary>设置内容文件值</summary>
public void Set(String DataSet, String DataName, String DataValue) {
String HaValue = DataName + "=" + DataValue.Replace("\n", @"\n");
Match HaMatchSet = new Regex(@"(^|\n)\[" + DataSet + @"\]\n(.|\n)*?($|(?=\n\[))").Match(FilesText);
if ( HaMatchSet.Success ) {
Match HaMatchName = new Regex(@"(^|\n)" + DataName + @"=.*?($|\n)").Match(HaMatchSet.Value);
if ( HaMatchName.Success ) {
if ( HaValue != "" ) {
HaValue = ( new Regex(@"^\n").Match(HaMatchName.Value).Success ) ? "\n" + HaValue : HaValue;
HaValue = ( new Regex(@"\n$").Match(HaMatchName.Value).Success ) ? HaValue + "\n" : HaValue;
FilesText = FilesText.Substring(0, HaMatchSet.Index + HaMatchName.Index) + HaValue + FilesText.Substring(HaMatchSet.Index + HaMatchName.Index + HaMatchName.Length);
} else {
FilesText = FilesText.Substring(0, HaMatchSet.Index + HaMatchName.Index) + FilesText.Substring(HaMatchSet.Index + HaMatchName.Index + HaMatchName.Length);
}
return;
}
if ( DataValue != "" ) {
HaValue = ( new Regex(@"\n$").Match(HaMatchSet.Value).Success ) ? HaValue : "\n" + HaValue;
FilesText = FilesText.Substring(0, HaMatchSet.Index + HaMatchSet.Length) + HaValue + FilesText.Substring(HaMatchSet.Index + HaMatchSet.Length);
} else {
FilesText = FilesText.Substring(0, HaMatchSet.Index + HaMatchSet.Length) + FilesText.Substring(HaMatchSet.Index + HaMatchSet.Length);
}
return;
}
if ( DataValue != "" ) {
String HaSet = "[" + DataSet + "]\n";
if ( FilesText != "" ) {
HaSet = ( new Regex(@"\n$").Match(FilesText).Success ) ? HaSet : "\n" + HaSet;
}
FilesText += HaSet + HaValue;
Console.WriteLine(FilesText + "\n" + System.DateTime.Now + ":" + System.DateTime.Now.Millisecond);
return;
}
}
///<summary>获取内容文件值</summary>
public String Get(String DataSet, String DataName) {
String Result = "";
try {
Result = FilesText;
Result = new Regex(@"(^|\n)\[" + DataSet + @"\]\n(.|\n)*?($|(?=\n\[))").Match(Result).Value;
Result = new Regex(@"(^|\n)" + DataName + @"=.*?($|\n)").Match(Result).Value;
Result = new Regex("^\n*" + DataName + "=|\n*$").Replace(Result, "");
Result = new Regex(@"\n").Replace(Result, "\n");
} catch {
Result = "";
}
return Result;
}
///<summary>重新打开配置文件</summary>
public void Open() {
for ( Int32 i = 0; i < 10; i++ ) {
try {
if ( File.Exists(AttrFiles) ) {
using ( FileStream FileStreams = new FileStream(AttrFiles, FileMode.Open) ) {
Byte[] HaByte = new Byte[(Int32)FileStreams.Length];
FileStreams.Read(HaByte, 0, (Int32)FileStreams.Length);
FilesText = Encoding.Default.GetString(HaByte, 0, HaByte.Length);
FilesText = new Regex(@"(\r|\n){2}|(\r|\n){1}").Replace(FilesText, "\n");
}
return;
}else{
FilesText = "";
}
} catch {
System.Threading.Thread.Sleep(200);
}
}
MessageBox.Show("读取配置文件失败." + AttrFiles);
}
///<summary>写入配置文件</summary>
public void Write() {
for ( Int32 i = 0; i < 10; i++ ) {
try {
using ( FileStream FileStreams = new FileStream(AttrFiles, FileMode.Create) ) {
Byte[] HaByte = Encoding.Default.GetBytes(FilesText.Replace("\n", "\r\n"));
FileStreams.Write(HaByte, 0, HaByte.Length);
}
return;
} catch {
System.Threading.Thread.Sleep(200);
}
}
MessageBox.Show("写入配置文件失败.");
}
///<summary>删除某个组</summary>
public void DelSet(String DataSet) {
Match HaMatchSet = new Regex(@"(^|\n)\[" + DataSet + @"\]\n(.|\n)*?($|(?=\n\[))").Match(FilesText);
String HaText = ( new Regex(@"\n$").Match(HaMatchSet.Value).Success ) ? "\n" : "";
FilesText = FilesText.Substring(0, HaMatchSet.Index) + HaText + FilesText.Substring(HaMatchSet.Index + HaMatchSet.Length);
}
}