using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
namespace Fractalist.Common
{
///
/// 操作INI文件的类
///
public class OperateIni
{
public OperateIni(){}
private const string defaultValue = "无法读取到指定的值";
private static StringBuilder retVal = new StringBuilder(255);
///
/// Win32读取INI文件的API函数
///
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
///
/// 读取INI配置文件的方法
///
/// 段落名称
/// 关键字
/// 无法读取时的缺省值
/// 读取到的值
/// 值大小
/// INI文件的完整路径和名称
///
private static int getPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath)
{
return GetPrivateProfileString(section, key, def, retVal, size, filePath);
}
///
/// Win32修改INI文件的API函数
///
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
///
/// 写入INI配置文件的方法(若为只读则先取消只读文件属性,写入文件,再修改文件属性为只读)
///
/// 段落名称
/// 关键字
/// 待写入的值
/// INI文件的完整路径和名称
///
private static long setPrivateProfileString(string section, string key, string val, string filePath)
{
long longVal = 0;
if((FileAttributes.ReadOnly & File.GetAttributes(filePath)) == FileAttributes.ReadOnly)
{
File.SetAttributes(filePath,FileAttributes.Normal);
longVal = WritePrivateProfileString(section, key, val, filePath);
File.SetAttributes(filePath,FileAttributes.ReadOnly);
}
else
{
longVal = WritePrivateProfileString(section, key, val, filePath);
}
return longVal;
}
private static string getValueOnly(string val)
{
string[] testArray = val.Split(new char[]{'#',});
return testArray[0];
}
}
}