[RELEASE] OpenSource INI Format Read/Write Class

Ceka

New member
Joined
Jun 25, 2011
Messages
2
Programming Experience
Beginner
I'd like to share a small project i worked yesterday on, Hopefully it will be used.
I'll explain the basic functionality here. :)

<<==Setup for using a INI Formated File ==>>


C#:
// Where the other usings are.
using iniControll;

// Creating a new instance of the INI Class
INI myIni = new INI("location to file here"); // Warning: Exceptions are not handled so, you're gonna have to check for your self.

<<==Using INI.ReadKey==>>

Let's say this is the file we have. And we want to read from 'key2'
C#:
key1 = hello
key2 = there
keyseven = whatever

C#:
// Use the code from the previous tutorial to already create an instance of the INI class
myIni.ReadKey("key2");
//Example of using the output :
//Console.WriteLine(myIni.ReadKey("key2"));

<<==Using INI.WriteKey==>>

Notes: Read the INI.ReadKey part of this Thread, or else you won't know where some things came from ^^
C#:
// Now, WriteKey will Replace a existing key if it already exists. Lets say we want to: Create a new key: key6 and change keyseven's value
myIni.WriteKey("keyseven","its7");
myIni.WriteKey("key6"."its6");

Have fun using this! ^^

Just make a new Project and add a class, Remove all of its default inerts and copy + paste this in; Then Compile it and Use it in your projects ^^!

C#:
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace iniControll
{
    public class INI
    {
        static string file;
        static string line;
        System.IO.StreamReader filez;

        public INI(string _file)
        {
            filez =  new System.IO.StreamReader(_file);
            filez.Close();
            file = _file;
        }
        public string ReadKey(string key)
        {
            filez = new System.IO.StreamReader(file);
            string result = null;
            
            while ((line = filez.ReadLine()) != null)
            {
                if (line.StartsWith(key) & (line[key.Length] == ' ' | line[key.Length] == '='))
                    {
                        int stage = 0;
                        
                        for (int i = key.Length; i<line.Length; i++)
                        {

                            if (line[i] == ' ') continue;
                            else if (line[i] == '=')  stage = 1;
                            else if (stage == 1)
                            {
                                result += line[i];
                            }
                        }
                    }
            
            }
            filez.Close();
            if (result == string.Empty) result = "";
            return result;

        }
        public void WriteKey(string key, string value)
        {
            System.IO.StreamWriter s = new System.IO.StreamWriter(file + ".wr");
            s.AutoFlush = true;


            filez = new System.IO.StreamReader(file);
            int b = 0;
            while ((line = filez.ReadLine()) != null)
            {
                if (line.StartsWith(key) & (line[key.Length] == ' ' | line[key.Length] == '='))
                {
                    s.WriteLine("{0} = {1}", key, value);
                    b = 1;
                }
                else
                {
                    s.WriteLine(line);
                }
            }
            if (b == 0)
            {
                s.WriteLine("{0} = {1}", key, value);
            }
            s.Close();
            filez.Close();
            System.IO.File.Replace(file + ".wr", file, file + ".bac");
            System.IO.File.Delete(file + ".bac");

            
        }
    }
}
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,562
Location
Norway
Programming Experience
10+

Henry Minute

New member
Joined
Apr 23, 2011
Messages
1
Programming Experience
10+
I am also a newbie to this site but even I can see that this has nothing to do with Component Development. Also as JohnH has said large parts of the functionality of INI files is missing.
 

Ceka

New member
Joined
Jun 25, 2011
Messages
2
Programming Experience
Beginner
Indeed, i really did not know where i should post this.
However, this is just a little test project on my way of learning C#. I never really thought it should be used for a serious project. It can tho be used for "setting" files an similar.

It's just a small useful "snippet"
 
Top Bottom