Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1562 | Anzahl registrierter User: 1893 | Besucher online: 145
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)

Einfache Ini Klasse


Autor: Legion
Sprache: C#
Bewertung:
noch nicht bewertet
Anzahl der Aufrufe: 3059
  
Kick it on dotnet-kicks.de  

Beschreibung:

Heute habe ich für euch eine kleine aber feine klasse für Ini-Dateien mit Hilfe von Dictionaries, welche ich vor ein paar Minuten soweit erstmal fertiggestellt habe. Grundlegendem Fehlerabfang ist soweit implementiert, diesmal per Exceptions. Ein paar Erweiterungen werden sich dafür aber bestimmt noch finden lassen, wie z.b. Kommentierte Zeilen mitnehmen (natürlich mit der korrekten Position in der Inidatei), oder Export/Import per Xml.
Verbesserungsvorschläge und so weiter, sind wie immer willkommen.


Abgelegt unter: Ini, Dictionary, C#, IniEntry, IniCategory.



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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
    /// <summary>
    /// 
    /// </summary>
    public sealed class Ini
    {
        /// <summary>
        /// 
        /// </summary>
        public Ini()
        {
            this._Categories = new Dictionary<String,IniCategory>();
            this._General = new IniCategory(String.Empty);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="File"></param>
        public Ini(String File) : this()
        {
            this.Read(File);
        }
        /// <summary>
        /// 
        /// </summary>
        public String Name
        {
            get
            {
                return this._Name;
            }
            set
            {
                this._Name = value;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="File"></param>
        public void Read(String File)
        {
            if (!System.IO.File.Exists(File)) throw new System.IO.FileNotFoundException("The specified path doesn't exist");

            this._Name = System.IO.Path.GetFileNameWithoutExtension(File);

            using (System.IO.StreamReader Input = new System.IO.StreamReader(File))
            {
                String Line = String.Empty;
                String Temp = String.Empty;
                IniCategory TCategory = this._General;

                while (!Input.EndOfStream)
                {
                    Line = Input.ReadLine();
                    //Skip comment lines and empty lines
                    if (Line.StartsWith(";") || String.IsNullOrEmpty(Line)) continue;

                    //Detect categories
                    if (Line.StartsWith("[") && Line.EndsWith("]"))
                    {
                        TCategory = this.CreateCategory(Line.Substring(1, Line.Length - 2));
                        continue;
                    }
                    //Detect invalid entries
                    if (!Line.Contains('=')) continue;
                    Temp = Line.Split('=')[0];
                    //Detect duplicate entries
                    if (TCategory._Entries.ContainsKey(Temp))
                    {
                        //Something to do
                    }
                    else
                    {
                        TCategory.CreateEntry(Temp, Line.Substring(Temp.Length + 1));
                    }
                }
            }       
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="File"></param>
        public void Save(String File)
        {
            using (System.IO.StreamWriter Output = new System.IO.StreamWriter(File, false))
            {
                foreach (IniEntry Entry in this._General._Entries.Values)
                {
                    Output.WriteLine(Entry.Name + "=" + Entry.Value);
                }
                foreach (IniCategory Category in this._Categories.Values)
                {
                    Output.WriteLine("[" + Category.Name + "]");
                    foreach (IniEntry Entry in Category._Entries.Values)
                    {
                        Output.WriteLine(Entry.Name + "=" + Entry.Value);
                    }
                }
            }            
        }
        /// <summary>
        /// 
        /// </summary>
        public IniCategory[] Categories
        {
            get
            {
                return this._Categories.Values.ToArray();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public IniEntry[] Entries
        {
            get
            {
                List<IniEntry> TEntries = new List<IniEntry>();
                TEntries.AddRange(this._General.Entries);
                foreach (IniCategory TCategory in this._Categories.Values)
                {
                    TEntries.AddRange(TCategory.Entries);
                }
                return TEntries.ToArray();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public IniCategory General
        {
            get
            {
                return this._General;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Category"></param>
        /// <returns></returns>
        public IniCategory this[String Category]
        {
            get
            {
                if (!this._Categories.ContainsKey(Category)) throw new InvalidOperationException("The category doesn't exists");
                else return this._Categories[Category];
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Category"></param>
        /// <param name="Entry"></param>
        /// <returns></returns>
        public IniEntry this[String Category, String Entry]
        {
            get
            {
                try
                {
                    if (!this._Categories.ContainsKey(Category)) throw new InvalidOperationException("The category doesn't exists");
                    else if (!this._Categories[Category]._Entries.ContainsKey(Entry)) throw new InvalidOperationException("The entry doesn't exists");
                    else return this._Categories[Category]._Entries[Entry];
                }
                catch
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        public IniCategory CreateCategory(String Name)
        {
            if (String.IsNullOrEmpty(Name)) throw new InvalidOperationException("The name of a category cant be empty");
            else if (this._Categories.ContainsKey(Name)) throw new InvalidOperationException("The category you tried to create already exists");

            return this._Categories[Name] = new IniCategory(Name);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        public IniEntry CreateEntry(String Name)
        {
            if (Name.Contains("=")) throw new InvalidOperationException("The name can't contain =");

            if (this._General._Entries.ContainsKey(Name)) throw new InvalidOperationException("The entry you tried to create already exists");
            return this._General._Entries[Name] = new IniEntry(Name, String.Empty);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Category"></param>
        /// <param name="Name"></param>
        public IniEntry CreateEntry(String Category, String Name)
        {
            return this.CreateEntry(Category, Name, String.Empty);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Category"></param>
        /// <param name="Name"></param>
        /// <param name="Value"></param>
        public IniEntry CreateEntry(String Category, String Name, String Value)
        {
            if (!this._Categories.ContainsKey(Category)) throw new InvalidOperationException("The category doesn't exists");
            else if (this._Categories[Category]._Entries.ContainsKey(Name)) throw new InvalidOperationException("The entry you tried to create already exists");
            else if (Name.Contains("=")) throw new InvalidOperationException("The name can't contain =");
            else return this._Categories[Category]._Entries[Name] = new IniEntry(Name, Value);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Value"></param>
        public void SetEntry(String Name, String Value)
        {
            if (Name.Contains("=")) throw new InvalidOperationException("The name can't contain =");

            if (this._General._Entries.ContainsKey(Name)) this._General._Entries[Name].Value = Value;
            else this._General._Entries[Name] = new IniEntry(Name, Value);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Category"></param>
        /// <param name="Name"></param>
        /// <param name="Value"></param>
        public void SetEntry(String Category, String Name, String Value)
        {
            if (String.IsNullOrEmpty(Category)) throw new InvalidOperationException("The name of a category can't be empty");
            else if (String.IsNullOrEmpty(Name)) throw new InvalidOperationException("The name of an entry can't be empty");
            else if (Name.Contains("=")) throw new InvalidOperationException("The name can't contain =");
            
            
            IniCategory TCategory = null;

            if (!this._Categories.ContainsKey(Category)) TCategory = this._Categories[Category] = new IniCategory(Category);
            else TCategory = this._Categories[Category];

            if (!TCategory._Entries.ContainsKey(Name)) TCategory._Entries[Name] = new IniEntry(Name, Value);
            else TCategory._Entries[Name].Value = Value;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public bool CategoryExists(String Name)
        {
            return this._Categories.ContainsKey(Name);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public bool EntryExists(String Name)
        {
            return this._General._Entries.ContainsKey(Name);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Category"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        public bool EntryExists(String Category, String Name)
        {
            return this._Categories[Category]._Entries.ContainsKey(Name);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        public void RemoveCategorie(String Name)
        {
            if (String.IsNullOrEmpty(Name)) throw new InvalidOperationException("The name of a category can't be empty");
            else this._Categories.Remove(Name);
        }
        /// <summary>
        /// Removes an entries von the categorized-entries
        /// </summary>
        /// <param name="Category"></param>
        /// <param name="Name"></param>
        public void RemoveEntry(String Category, String Name)
        {
            if (this._Categories.ContainsKey(Category)) this._Categories[Category]._Entries.Remove(Name);
        }
        /// <summary>
        /// Removes an entry from the uncategorized-entries
        /// </summary>
        /// <param name="Name"></param>
        public void RemoveEntry(String Name)
        {
            this._General.RemoveEntry(Name);
        }
        /// <summary>
        /// Represent the categories
        /// </summary>
        internal Dictionary<String, IniCategory> _Categories;
        /// <summary>
        /// Represent the Entries without category
        /// </summary>
        internal IniCategory _General;
        /// <summary>
        /// 
        /// </summary>
        private String _Name;
    }
    /// <summary>
    /// 
    /// </summary>
    public sealed class IniCategory
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Ini"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        public static IniCategory Create(Ini Ini, String Name)
        {
            if (Ini == null) throw new ArgumentNullException("The ini object can't be null");
            else if (Ini._Categories.ContainsKey(Name)) throw new InvalidOperationException("The category you tried to create already exists");
            else if (String.IsNullOrEmpty(Name)) throw new InvalidOperationException("The name of a category can't be empty");
            return Ini.CreateCategory(Name);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        internal IniCategory(String Name)
        {
            this._Name = Name;
            this._Entries = new Dictionary<String, IniEntry>();
        }
        /// <summary>
        /// 
        /// </summary>
        public String Name
        {
            get
            {
                return this._Name;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Entry"></param>
        /// <returns></returns>
        public IniEntry this[String Entry]
        {
            get
            {
                try
                {
                    return this._Entries[Entry];
                }
                catch
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public IniEntry[] Entries
        {
            get
            {
                return this._Entries.Values.ToArray();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        public IniEntry CreateEntry(String Name)
        {
            return this.CreateEntry(Name, String.Empty);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Value"></param>
        public IniEntry CreateEntry(String Name, String Value)
        {
            if(this._Entries.ContainsKey(Name)) throw new InvalidOperationException("The entry you tried to create already exists");
            else if (String.IsNullOrEmpty(Name)) throw new InvalidOperationException("The name of an entry can't be empty");
            else if (Name.Contains("=")) throw new InvalidOperationException("The name can't contain =");
            return (this._Entries[Name] = new IniEntry(Name, Value));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        public void RemoveEntry(String Name)
        {
            this._Entries.Remove(Name);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Value"></param>
        public void SetEntry(String Name, String Value)
        {
            if (Name.Contains("=")) throw new InvalidOperationException("The name can't contain =");

            if(!this._Entries.ContainsKey(Name)) this._Entries[Name] = new IniEntry(Name, Value);
            else this._Entries[Name].Value = Value;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public bool EntryExists(String Name)
        {
            return this._Entries.ContainsKey(Name);
        }
        /// <summary>
        /// 
        /// </summary>
        public bool HasEntries
        {
            get
            {
                return (this._Entries.Count > 0);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private String _Name;
        /// <summary>
        /// 
        /// </summary>
        internal Dictionary<String, IniEntry> _Entries;
    }
    /// <summary>
    /// 
    /// </summary>
    public sealed class IniEntry
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Category"></param>
        /// <param name="Name"></param>
        /// <param name="Value"></param>
        public static IniEntry Create (IniCategory Category, String Name, String Value)
        {
            if (Category == null) throw new ArgumentNullException("The category object can't be null");
            else if (Category._Entries.ContainsKey(Name)) throw new InvalidOperationException("The entry you tried to create already exists");
            else if (String.IsNullOrEmpty(Name)) throw new InvalidOperationException("The name of an entry can't be empty");
            else if (Name.Contains("=")) throw new InvalidOperationException("The name can't contain =");
            
            return Category.CreateEntry(Name, Value);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Value"></param>
        internal IniEntry(String Name, String Value)
        {
            this._Name = String.Intern(Name);
            this._Value = String.Intern(Value);
        }
        /// <summary>
        /// 
        /// </summary>
        public String Name
        {
            get
            {
                return this._Name;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public String Value
        {
            get
            {
                return this._Value;
            }
            set
            {
                this._Value = value;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public bool HasValue
        {
            get
            {
                return !String.IsNullOrEmpty(this._Value);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private String _Name;
        /// <summary>
        /// 
        /// </summary>
        private String _Value;
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this._Value;
        }
    }
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.)



Diese Snippets könnten für Sie interessant sein:
[VB.NET] INI Wert lesen
[VB.NET] INI Werte enumerieren
[VB.NET] INI Eintrag löschen
[VB.NET] INI Sektion löschen
[VB.NET] INI Wert schreiben
[C#] INI - Dateien lesen und schreiben
[VB.NET] Klasse für Verwendung von INI-Dateien
[C#] Ini Reader
[C#] Ini-Datei-Klasse
[VB.NET] Neu: Klasse für Verwendung von INI-Dateien
[C#] Erweiterte Ini-Datei-Klasse
[C#] ini Datei anlegen - mit Dictionary
[C#] Filtern mit dem Blacklist - Whitelist Verfahren
[C#] Add Key-Value return old Value
[C#] Ein kleiner Konsolen Taschenrechner
[C#] Prüft Zeitformat auf 24 Stunden
[C#] Datei/Ordner durch Erweiterungsmethoden abfragen
[C#] GetCharts
[C#] XML in DataTable laden
[C#] Generische Between Methode
[ASP.net] Formularelement fokusieren auf Ajax Seiten
[C#] Quadratwurzel ziehen
[C#] Cast to Enum
[C#] ASP.NET: Metatags und Titel zur Laufzeit setzen
[C#] Aufrufenden Typ identifizieren
[C#] Kaufmännisches Runden in Decimal
[C#] WMI Getter
[C#] Sql Server Instanzen finden
[C#] WPF: Image aus dem Web in Image-Element anzeigen
[C#] Quoted Printable Encoder
[C#] URL auslesen
[C#] User-Verwaltung in eine ASP.NET Website einbauen
[C#] GUID des aktuellen Assembly ermitteln
[C#] Windows Forms Anwendungs-Sessions verwalten
[C#] User-Management für WPF Smart Client Anwendung
[C#] Silverlight Anwendung erstellen
[C#] User-Verwaltung in eine WinForm einbauen
[C#] Java WebService aus einem WinForms-Client aufrufen
[C#] C# Ist ein POINT zwischen ... und ...
[C#] NumTextBox (Kurzversion)
[C#] Another Bin-Watch (Console)
[C#] SystemIconsImageListWrapper
[C#] WatchedList
[C#] Datenbankverbindung herstellen
[C#] Youtube Download Klasse für C#
[C#] InputBox WPF C#
[C#] C# Ordner auslesen und in Liste speichern "rekursiv"
[C#] C# XOR Crypt

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