资源描述
//
//INIClass.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace BoitBoy.MyFile
{
/// <summary>
/// INI设置文件类
/// </summary>
public class INIClass
{
public string inipath;
/// <summary>
/// 分隔符
/// </summary>
public static String SpeatorString = “,-,”;
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[System.Runtime.InteropServices.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, object Value)
{
if (Value == null)
return;
if (Value.GetType() != typeof(System.Collections.ArrayList))
WritePrivateProfileString(Section, Key, Value.ToString(), this.inipath);
else
{
System.Collections.ArrayList TempArray = Value as System.Collections.ArrayList;
String StringValue = “”;
if (TempArray != null)
{
foreach (object Temp in TempArray)
{
StringValue += Temp.ToString() + SpeatorString;
}
}
IniWriteValue(Section, Key, StringValue);
}
}
/// <summary>
/// 读出INI文件
/// </summary>
/// <param name=”Section”>项目名称(如 [TypeName] )</param>
/// <param name=”Key”>键</param>
public string IniReadValue_S(string Section, string Key)
{
StringBuilder temp = new StringBuilder(500);
int i = GetPrivateProfileString(Section, Key, “”, temp, 500, this.inipath);
return temp.ToString();
}
public double IniReadValue(string Section, string Key)
{
String Temp = IniReadValue_S(Section, Key);
double temp = 0;
double.TryParse(Temp, out temp);
return temp;
}
public int IniReadValue_I(string Section, string Key)
{
String Temp = IniReadValue_S(Section, Key);
int temp = 0;
int.TryParse(Temp, out temp);
return temp;
}
/// <summary>
/// 读物数组
/// </summary>
/// <param name=”Section”></param>
/// <param name=”Key”></param>
/// <returns></returns>
public System.Collections.ArrayList IniReadValue_A(string Section, string Key)
{
String Value = IniReadValue_S(Section, Key);
String[] Splitor = new String[1];
Splitor[0] = SpeatorString;
String[] Values = Value.Split(Splitor, StringSplitOptions.RemoveEmptyEntries);
System.Collections.ArrayList Back = new System.Collections.ArrayList();
foreach (String Temp in Values)
{
Back.Add(Temp);
}
return Back;
}
/// <summary>
/// 验证文件是否存在
/// </summary>
/// <returns>布尔值</returns>
public bool ExistINIFile()
{
return System.IO.File.Exists(inipath);
}
}
}
展开阅读全文