using System;
using System.Text;
using System.Runtime.InteropServices;
public class INIHelper
{
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileSectionNames(byte[] retVal, int size, string filePath);
private readonly string strFileName;
public string FileName
{
get{return strFileName;}
}
public INIHelper(string Filename)
{
strFileName = System.IO.Path.GetFullPath(Filename);
}
public byte[] GetSectionBytes()
{
byte[] val = new byte[32768];
int size = GetPrivateProfileSectionNames(val, 32768, strFileName);
byte[] retval = new byte[size];
Array.Copy(val, 0, retval, 0, size);
return retval;
}
public string[] Sections
{
get
{
byte[] sec = GetSectionBytes();
string strs = System.Text.Encoding.Default.GetString(sec, 0, sec.Length);
return strs.ToUpper().Split('\0');
}
}
public bool SectionExist(string Section)
{
Section = Section.Replace(" ", "");
if (Section.Equals(""))
{
return false;
}
else
{
return Array.IndexOf(Sections, Section.ToUpper()) != -1;
}
}
public void RemoveSection(string Section)
{
if (Section != null)
{
WritePrivateProfileString(Section, null, null, strFileName);
}
}
public void RemoveKey(string Section, string Key)
{
if (Section != null && Key != null)
{
WritePrivateProfileString(Section, Key, null, strFileName);
}
}
public void Write(string Section, string Key, string Value)
{
if (Section != null && Key != null && Value != null)
{
WritePrivateProfileString(Section, Key, Value, strFileName);
}
}
public void Write(string Section, string Key, float Value)
{
Write(Section, Key, Value.ToString());
}
public void Write(string Section, string Key, int Value)
{
Write(Section, Key, Value.ToString());
}
public void Write(string Section, string Key, bool Value)
{
Write(Section, Key, Value.ToString());
}
public string Read(string Section, string Key, string DefaultValue)
{
StringBuilder val = new StringBuilder(32768);
GetPrivateProfileString(Section, Key, DefaultValue, val, 8192, strFileName);
if (Section.Equals(null) || Key.Equals(null))
{
return string.Empty;
}
return val.ToString();
}
public bool Read(string Section, string Key, bool DefaultValue)
{
string defaultvalue = "False";
bool retvalue = false;
try
{
defaultvalue = DefaultValue.ToString();
retvalue = bool.Parse(Read(Section, Key, defaultvalue));
}
catch
{
}
return retvalue;
}
public int Read(string Section, string Key, int DefaultValue)
{
string defaultvalue = "0";
int retvalue = 0;
try
{
defaultvalue = DefaultValue.ToString();
retvalue = int.Parse(Read(Section, Key, defaultvalue));
}
catch
{
}
return retvalue;
}
public float Read(string Section, string Key, float DefaultValue)
{
string defaultvalue = "0";
float retvalue = 0;
try
{
defaultvalue = DefaultValue.ToString();
retvalue = float.Parse(Read(Section, Key, defaultvalue));
}
catch
{
}
return retvalue;
}
}
using System.Text;
using System.Runtime.InteropServices;
public class INIHelper
{
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileSectionNames(byte[] retVal, int size, string filePath);
private readonly string strFileName;
public string FileName
{
get{return strFileName;}
}
public INIHelper(string Filename)
{
strFileName = System.IO.Path.GetFullPath(Filename);
}
public byte[] GetSectionBytes()
{
byte[] val = new byte[32768];
int size = GetPrivateProfileSectionNames(val, 32768, strFileName);
byte[] retval = new byte[size];
Array.Copy(val, 0, retval, 0, size);
return retval;
}
public string[] Sections
{
get
{
byte[] sec = GetSectionBytes();
string strs = System.Text.Encoding.Default.GetString(sec, 0, sec.Length);
return strs.ToUpper().Split('\0');
}
}
public bool SectionExist(string Section)
{
Section = Section.Replace(" ", "");
if (Section.Equals(""))
{
return false;
}
else
{
return Array.IndexOf(Sections, Section.ToUpper()) != -1;
}
}
public void RemoveSection(string Section)
{
if (Section != null)
{
WritePrivateProfileString(Section, null, null, strFileName);
}
}
public void RemoveKey(string Section, string Key)
{
if (Section != null && Key != null)
{
WritePrivateProfileString(Section, Key, null, strFileName);
}
}
public void Write(string Section, string Key, string Value)
{
if (Section != null && Key != null && Value != null)
{
WritePrivateProfileString(Section, Key, Value, strFileName);
}
}
public void Write(string Section, string Key, float Value)
{
Write(Section, Key, Value.ToString());
}
public void Write(string Section, string Key, int Value)
{
Write(Section, Key, Value.ToString());
}
public void Write(string Section, string Key, bool Value)
{
Write(Section, Key, Value.ToString());
}
public string Read(string Section, string Key, string DefaultValue)
{
StringBuilder val = new StringBuilder(32768);
GetPrivateProfileString(Section, Key, DefaultValue, val, 8192, strFileName);
if (Section.Equals(null) || Key.Equals(null))
{
return string.Empty;
}
return val.ToString();
}
public bool Read(string Section, string Key, bool DefaultValue)
{
string defaultvalue = "False";
bool retvalue = false;
try
{
defaultvalue = DefaultValue.ToString();
retvalue = bool.Parse(Read(Section, Key, defaultvalue));
}
catch
{
}
return retvalue;
}
public int Read(string Section, string Key, int DefaultValue)
{
string defaultvalue = "0";
int retvalue = 0;
try
{
defaultvalue = DefaultValue.ToString();
retvalue = int.Parse(Read(Section, Key, defaultvalue));
}
catch
{
}
return retvalue;
}
public float Read(string Section, string Key, float DefaultValue)
{
string defaultvalue = "0";
float retvalue = 0;
try
{
defaultvalue = DefaultValue.ToString();
retvalue = float.Parse(Read(Section, Key, defaultvalue));
}
catch
{
}
return retvalue;
}
}
浙公网安备 33010602011771号