Sprache: C#
Mit dieser Klasse kann sehr einfach lesend und schreibend auf INI – Dateien zugegriffen werden.
public class IniFile
{
public string path;
[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);
public IniFile(string INIPath)
{
path = INIPath;
}
public void IniWriteValue(string Section,string Key,string Value)
{
WritePrivateProfileString(Section,Key,Value,this.path);
}
public string IniReadValue(string Section,string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section,Key,"",temp,255, this.path);
return temp.ToString();
}
}
public class IniFile
{
public string path;
[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);
public IniFile(string INIPath)
{
path = INIPath;
}
public void IniWriteValue(string Section,string Key,string Value)
{
WritePrivateProfileString(Section,Key,Value,this.path);
}
public string IniReadValue(string Section,string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section,Key,"",temp,255, this.path);
return temp.ToString();
}
}
Alte URL:
/snippet/ini-dateien-lesen-und-schreiben/60
I modded your Snippet a little:
[code]using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace JB.IO
{
// Source: http://dotnet-snippets.de/dns/ini—dateien-lesen-und-schreiben-SID60.aspx
// Changed by JB: http://jbquerier.blogspot.com
///
///
public class IniFile
{
// JB: accessor changed.
///
///
private string path;
[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);
///
///
/// Pfad zur Ini-Datei
public IniFile(string INIPath)
{
path = INIPath;
}
// Part by JB
///
///
/// Name der SektionSektion
///
public Section this[string Section]
{
get { return new Section(Section, this); }
}
// Part by JB
///
///
public class Section
{
private string Name;
private IniFile File;
///
///
/// Name der Sektion
/// Verweis auf die Ini-Datei
public Section(string name, IniFile file)
{
Name=name;
File = file;
}
///
///
/// Bezeichner des WertesString des Wertes
///
public string this[string Key]
{
get { return File.IniReadValue(Name, Key); }
set { if (value != this[Key]) File.IniWriteValue(Name, Key, value); }
}
}
// JB: accessor changed.
///
///
/// Name der Sektion
/// Bezeichner des Wertes
/// Wert
protected void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.path);
}
// JB: accessor changed.
///
///
/// Name der SektionWert
/// Bezeichner des Wertes
///
protected string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, „“, temp, 255, this.path);
return temp.ToString();
}
}
}
// Usage-Sample:
public partial class MainWindow : Window
{
private JB.IO.IniFile Settings;
public MainWindow()
{
InitializeComponent();
Settings = new JB.IO.IniFile(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @“Demo.ini“);
}
private void IniWrite_Click(object sender, RoutedEventArgs e)
{
Settings[„DemoSection“][„DemoCaption“] = „DemoValue“;
}
private void IniRead_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(Settings[„DemoSection“][„DemoCaption“]);
}
}
[/code]