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 ==>>
<<==Using INI.ReadKey==>>
Let's say this is the file we have. And we want to read from 'key2'
<<==Using INI.WriteKey==>>
Notes: Read the INI.ReadKey part of this Thread, or else you won't know where some things came from ^^
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 ^^!
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");
}
}
}