You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
3.9 KiB
106 lines
3.9 KiB
using System; |
|
using System.Collections.Generic; |
|
using System.Text; |
|
using System.Runtime.InteropServices; |
|
using System.IO; |
|
|
|
namespace UTIL |
|
{ |
|
#region Работа с INI файлами |
|
public class IniFile |
|
{ |
|
[DllImport("kernel32.dll")] |
|
private extern static int GetPrivateProfileString(String AppName, String KeyName, String Default, StringBuilder ReturnedString, UInt32 Size, String FileName); |
|
[DllImport("kernel32.dll")] |
|
private extern static int WritePrivateProfileString(String AppName, String KeyName, String Str, String FileName); |
|
public IniFile(string filename) |
|
{ |
|
IniFileName = filename; |
|
//if (!File.Exists(filename)) |
|
// File.Create(filename); |
|
IniFileName = new FileInfo(IniFileName).FullName.ToString(); |
|
} |
|
public String IniFileName |
|
{ |
|
get; |
|
set; |
|
} |
|
|
|
//Читаем ini-файл и возвращаем значение указного ключа из заданной секции. |
|
public string ReadINI(string Section, string Key) |
|
{ |
|
var RetVal = new StringBuilder(255); |
|
if (GetPrivateProfileString(Section, Key, "", RetVal, 255, IniFileName).ToString().Length > 0) |
|
return RetVal.ToString(); |
|
else |
|
return null; |
|
} |
|
//Записываем в ini-файл. Запись происходит в выбранную секцию в выбранный ключ. |
|
public void Write(string Section, string Key, string Value) |
|
{ |
|
WritePrivateProfileString(Section, Key, Value, IniFileName); |
|
} |
|
public void WriteINI(string Section, string Key, string Value) |
|
{ |
|
WritePrivateProfileString(Section, Key, Value, IniFileName); |
|
} |
|
|
|
//Удаляем ключ из выбранной секции. |
|
public void DeleteKey(string Key, string Section = null) |
|
{ |
|
Write(Section, Key, null); |
|
} |
|
//Удаляем выбранную секцию |
|
public void DeleteSection(string Section = null) |
|
{ |
|
Write(Section, null, null); |
|
} |
|
//Проверяем, есть ли такой ключ, в этой секции |
|
public bool KeyExists(string Key, string Section = null) |
|
{ |
|
return ReadINI(Section, Key).Length > 0; |
|
} |
|
|
|
public String GetString(String section, String key) |
|
{ |
|
StringBuilder s1 = new StringBuilder(255); |
|
s1.Append(ReadINI(section, key).ToString()); |
|
return s1.ToString(); |
|
} |
|
public Int64 GetInt(String section, String key) |
|
{ |
|
StringBuilder s1 = new StringBuilder(255); |
|
s1.Append(ReadINI(section, key).ToString()); |
|
return Int64.Parse(s1.ToString()); |
|
} |
|
public Boolean GetBool(String section, String key) |
|
{ |
|
StringBuilder s1 = new StringBuilder(255); |
|
s1.Append(ReadINI(section, key).ToString()); |
|
return Boolean.Parse(s1.ToString()); |
|
} |
|
public Double GetDouble(String section, String key) |
|
{ |
|
StringBuilder s1 = new StringBuilder(255); |
|
s1.Append(ReadINI(section, key).ToString()); |
|
return Double.Parse(s1.ToString()); |
|
} |
|
public void SetString(String section, String key, String val) |
|
{ |
|
Write(section, key, val); |
|
} |
|
public void SetInt(String section, String key, Int64 val) |
|
{ |
|
Write(section, key, val.ToString()); |
|
} |
|
public void SetDouble(String section, String key, Double val) |
|
{ |
|
Write(section, key, val.ToString()); |
|
} |
|
public void SetBool(String section, String key, Boolean val) |
|
{ |
|
Write(section, key, val.ToString()); |
|
} |
|
} |
|
#endregion |
|
}
|
|
|