Entwickler-Ecke

IO, XML und Registry - IniFiles


fifi - Mi 15.04.09 13:09
Titel: IniFiles
Hallo Leute,
ich brauche mal Hilfe.wie kann ich eine klasse erstellen die das auslesen in speichern von daten aus/in INI Dateien vereinfacht?
Programmiersprache c#
ein paar tipps sind willkommen
danke im voraus


Moderiert von user profile iconKha: Topic aus WinForms verschoben am Mi 15.04.2009 um 14:21


Kha - Mi 15.04.09 14:24

:welcome:

Was genau ist nun deine Frage?
Natürlich kannst du gerne eine eigene Klasse dafür schreiben und wenn es an einer Stelle hakt, können wir dir auch weiterhelfen, aber es gibt auch schon fertig Klassen, z.B. diese hier: http://www.codeproject.com/KB/cs/cs_ini.aspx


fifi - Mi 15.04.09 16:51

ja habe den code angeschaut.Wie soll ich nun Daten in IniFiles speichern?
hier nochmals den code:



C#-Quelltext
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:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace Ini
{
    /// <summary>
    /// Create a New INI file to store or load data
    /// </summary>
    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);

        /// <summary>
        /// INIFile Constructor.
        /// </summary>
        /// <param name="INIPath"></param>
        public IniFile(string INIPath)
        {
            path = INIPath;
        }
        /// <summary>
        /// Write Data to the INI File
        /// </summary>
        /// <param name="Section"></param>
        /// Section name
        /// <param name="Key"></param>
        /// Key Name
        /// <param name="Value"></param>
        /// Value Name
        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.path);
        }

        /// <summary>
        /// Read Data Value From the Ini File
        /// </summary>
        /// <param name="Section"></param>
        /// <param name="Key"></param>
        /// <param name="Path"></param>
        /// <returns></returns>
        public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255this.path);
            return temp.ToString();

        }

    }
}

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Ini;

namespace IniTest
{


/// <summary>
  /// Summary description for Form1.
  /// </summary>
  public class Form1 : System.Windows.Forms.Form
  {
    private System.Windows.Forms.TextBox name;
    private System.Windows.Forms.TextBox lname;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public Form1()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.name = new System.Windows.Forms.TextBox();
      this.lname = new System.Windows.Forms.TextBox();
      this.SuspendLayout();
      // 
      // name
      // 
      this.name.Location = new System.Drawing.Point(88);
      this.name.Name = "name";
      this.name.Size = new System.Drawing.Size(18420);
      this.name.TabIndex = 0;
      this.name.Text = "Name";
      // 
      // lname
      // 
      this.lname.Location = new System.Drawing.Point(840);
      this.lname.Name = "lname";
      this.lname.Size = new System.Drawing.Size(18420);
      this.lname.TabIndex = 1;
      this.lname.Text = "Last Name";
      // 
      // Form1
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(513);
      this.ClientSize = new System.Drawing.Size(20870);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.lname,
                                      this.name});
      this.Name = "Form1";
      this.Text = "Form1";
      this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
      this.Load += new System.EventHandler(this.Form1_Load);
      this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
      IniFile ini = new IniFile("C:\\test.ini");
      ini.IniWriteValue("Info","Name",name.Text);
      ini.IniWriteValue("Info","LastName",lname.Text);
    }
    private void Form1_Load(object sender, System.EventArgs e)
    {
      IniFile ini = new IniFile("C:\\test.ini");
      name.Text= ini.IniReadValue("Info","Name");
      lname.Text = ini.IniReadValue("Info","LastName");
    }
  }
}


Danke

Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt


Kha - Mi 15.04.09 17:11

Mal abgesehen davon, dass die erste Hälfte hoffentlich in einer eigenen Datei steckt und du .NET 1.1 verwendest (?), sieht das von hier aus in Ordnung aus. Was passiert denn, bekommst du einen Fehler?


fifi - Mi 15.04.09 17:26

nein alles lauft perfekt.Ich soll jetzt nun eine methode die das speichern von Daten in Inifiles ermöglicht.hum es fehlt mir momentan nichts vernünftiges.


Findus - Fr 19.06.09 07:46

Sorry wenn ich diesesn Thread nochmals ausgrabe aber ich hätte da eine Frage zum "INIPath". Wie kann ich das ändern das er die INI im selben Verzeichnis schreibt/liest wo auch die exe liegt ???

Gibt man einen Path an geht es ja.

C#-Quelltext
1:
IniFile ini = new IniFile("C:\\test.ini");                    

Will ich es aber wie folgt versuchen geht es nicht.

C#-Quelltext
1:
IniFile ini = new IniFile(@"test.ini");                    


Gruß Findus


jaenicke - Fr 19.06.09 07:49

Schau dir einmal Application.StartupPath [http://msdn.microsoft.com/de-de/library/system.windows.forms.application.startuppath.aspx] an. ;-)


Findus - Fr 19.06.09 08:11

Danke und schon klappt es mit dem Nachbar .. :lol:


C#-Quelltext
1:
IniFile ini = new IniFile (Application.StartupPath + "\\test.ini");                    

Gruß Findus