Windows Azure Cloud Storage ermöglicht es Ihnen bereits ab 0,10€ pro GB/Monat die Vorteile der Cloud zu nutzen.
Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1549 | Anzahl registrierter User: 1833 | Besucher online: 1698
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

INI - Dateien lesen und schreiben


Autor: Jan Welker
Sprache: C#
Bewertung:
8.78 (9 votes)
Anzahl der Aufrufe: 33153
  
Kick it on dotnet-kicks.de  

Beschreibung:

Mit dieser Klasse kann sehr einfach lesend und schreibend auf INI - Dateien zugegriffen werden.

Abgelegt unter: Ini, lesen, schreiben.



C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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();
    }
  }
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)

rsc0flake schrieb am:  01.02.2011 09:43:44

I modded your Snippet a little:
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
/// <summary>
/// Klasse zum Schreiben von Ini-Dateien.
/// </summary>
public class IniFile
{
// JB: accessor changed.
/// <summary>
/// Pfad der Ini-Datei.
/// </summary>
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);

/// <summary>
/// Konstruktor
/// </summary>
/// <param name="INIPath">Pfad zur Ini-Datei</param>
public IniFile(string INIPath)
{
path = INIPath;
}

// Part by JB
/// <summary>
/// Sektion der Ini-Datei
/// </summary>
/// <param name="Section">Name der Sektion</param>
/// <returns>Sektion</returns>
public Section this[string Section]
{
get { return new Section(Section, this); }
}

// Part by JB
/// <summary>
/// Sektion-Struktur
/// </summary>
public class Section
{
private string Name;
private IniFile File;

/// <summary>
/// Konstruktor für die Sektion
/// </summary>
/// <param name="name">Name der Sektion</param>
/// <param name="file">Verweis auf die Ini-Datei</param>
public Section(string name, IniFile file)
{
Name=name;
File = file;
}

/// <summary>
/// Schreibt oder ermittelt einen Ini-Wert.
/// </summary>
/// <param name="Key">Bezeichner des Wertes</param>
/// <returns>String des Wertes</returns>
public string this[string Key]
{
get { return File.IniReadValue(Name, Key); }
set { if (value != this[Key]) File.IniWriteValue(Name, Key, value); }
}
}

// JB: accessor changed.
/// <summary>
/// Schreibt einen Ini-Wert
/// </summary>
/// <param name="Section">Name der Sektion</param>
/// <param name="Key">Bezeichner des Wertes</param>
/// <param name="Value">Wert</param>
protected void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.path);
}

// JB: accessor changed.
/// <summary>
/// Ermittelt einen Ini-Wert
/// </summary>
/// <param name="Section">Name der Sektion</param>
/// <param name="Key">Bezeichner des Wertes</param>
/// <returns>Wert</returns>
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"]);
}
}


schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.