22 Eylül 2010 Çarşamba

Configuration Files Reading & Writing

Öncelikle bir config dosyasından yaratılan section lardan okuma ve yazmayı bilinen bir şekilde yani şöyle;


Configuration conf =
 new Configuration(new System.IO.StreamReader("Configuration.ini"));
string UserName = conf.GetValue("User");
string HomePage = conf.GetValue("Internet_Data","Home_Page");

conf.SetValue( "User_Information", 
               "Log_In_Time",
               System.DateTime.Now.ToString()); 
 
gibi fakat aşağıda göreceğiniz kodlarla yazılacak ve yaratılacak library;
 
Tek fark bu kodu hızlı ve konfigürasyon dosyası kullanılmış olabileceği 
için veya değiştirilmiş sürecinde daha önce başka bir uygulama tarafından 
uzun süre bellekte saklanamaz. Yani, aşağıdaki kodu sadece sırayla dosyayı 
okur ve, o bölüm ya da anahtarı bulur, harekete geçmiş ve daha sonra orada 
kendisi dosyayı kapatır.Bu işlemin dezavantajı her işlev çağrısında 
bu dosya açılır ve veya değiştirmeden kapatılır. Bu kodlama kapalı olduğu 
için yapılandırma dosyası tamamlandıktan sonra fonksiyon birkaç kez 
değişiklikleri yansıtabilir.

aşağıdaki kodu bir class library yaratarak dll haline getirip istediğimiz platformda
kullanabiliriz.

using System;
using System.IO;
using System.Collections.Specialized;

namespace ReadWriteConfigFile
{
    public class Config
    {
        public static void SetIniString
(string sFile, string Sec,string Key, string Val)
        {
            StreamReader sr = null;
            string newLineChars = "\r\n";   

            if (true)
            {
                StringWriter tw = new StringWriter();
                newLineChars = tw.NewLine;
            }

            string slines = null;
            if (File.Exists(sFile))
            {
                bool reWrite = false;
                using (sr = new StreamReader(sFile))
                {
                    String line;
                    bool foundSection = false;
                    while ((line = sr.ReadLine()) != null)
                    {
                        line = line.Trim();
                        if (line.Length > 1)
                        {
                            if (line[0] == '[')
                            {
                                int end = line.IndexOf("]");
                                if (end != -1)
                                {
                                    string section = line.Substring(1,
                                                                    end - 1);
                                    if (String.Compare(section, Sec, true) == 0)
                                    {
                                        foundSection = true;
                                        slines += line + newLineChars;
                                        continue;
                                    }
                                    else
                                    {
                                        if (foundSection == true)
                                        {

                                            slines += Key + "=" + Val
                                                          + newLineChars; ;
                                            slines += line + newLineChars;
                                            line = sr.ReadToEnd();
                                            slines += line;
                                            reWrite = true;
                                            break;
                                        }
                                    }
                                }
                            }
                            if (foundSection == true)
                            {
                                string[] pair = line.Split('=');
                                if (pair.Length > 1)
                                {
                                    if (String.Compare(pair[0], Key, true)
                                              == 0)
                                    {
                                        line = Key + "=" + Val
                                                         + newLineChars;
                                        slines += line;
                                        line = sr.ReadToEnd();
                                        slines += line;
                                        reWrite = true;
                                        break;
                                    }
                                }
                            }
                        }
                        slines += line + newLineChars;
                    }
                    if (foundSection == false)
                    {
                        slines += "[" + Sec + "]" + newLineChars;
                        slines += Key + "=" + Val + newLineChars;
                        reWrite = true;
                    }
                    if (foundSection == true && reWrite == false)
                    {
                        slines += Key + "=" + Val + newLineChars;
                        reWrite = true;
                    }
                }
                if (reWrite == true)
                {
                    using (StreamWriter sw = new StreamWriter(sFile))
                    {
                        sw.Write(slines);
                    }
                }
            }
            else
            {
                slines = "[" + Sec + "]" + newLineChars;
                slines += Key + "=" + Val + newLineChars;
                using (StreamWriter sw = new StreamWriter(sFile))
                {
                    sw.Write(slines);
                }

            }
        }

        public static string GetIniString(string sFile, string Sec,string Key, string Val)
        {
            StreamReader sr = null;
            if (File.Exists(sFile))
            {
                using (sr = new StreamReader(sFile))
                {
                    String line;
                    bool foundSection = false;
                    while ((line = sr.ReadLine()) != null)
                    {
                        line = line.Trim();
                        if (line.Length > 1)
                        {
                            if (line[0] == '[')
                            {
                                int end = line.IndexOf("]");
                                if (end != -1)
                                {
                                    string section = line.Substring(1,
                                                                    end - 1);
                                    if (String.Compare(section, Sec, true)
                                       == 0)
                                    {
                                        foundSection = true;
                                        continue;
                                    }
                                    else
                                        foundSection = false;
                                }
                            }
                            if (foundSection == true)
                            {
                                string[] pair = line.Split('=');
                                if (pair.Length > 1)
                                {
                                    if (String.Compare(pair[0], Key, true)
                                        == 0)
                                    {
                                        Val = pair[1];
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return Val;
        }
        
        public static StringCollection GetSections(string sFile)
        {
            StreamReader sr = null;
            StringCollection myAL = new StringCollection();
            if (File.Exists(sFile))
            {

                using (sr = new StreamReader(sFile))
                {
                    String line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        line = line.Trim();
                        if (line.Length > 1)
                        {
                            if (line[0] == '[')
                            {
                                int end = line.IndexOf("]");
                                if (end != -1)
                                {
                                    string section = line.Substring(1,
                                                                    end - 1);
                                    myAL.Add(section);
                                }
                            }
                        }
                    }
                }
            }
            return myAL;
        }

        public static StringCollection GetSectionKeys(string sFile,string Sec)
        {
            StreamReader sr = null;
            StringCollection myAL = new StringCollection();
            if (File.Exists(sFile))
            {
                using (sr = new StreamReader(sFile))
                {
                    String line;
                    bool foundSection = false;
                    while ((line = sr.ReadLine()) != null)
                    {line = line.Trim();
                        if (line.Length > 1)
                        {
                            if (line[0] == '[')
                            {
                                int end = line.IndexOf("]");
                                if (end != -1)
                                {
                                    string section = line.Substring(1,
                                                                    end - 1);
                                    if (String.Compare(section, Sec, true)
                                       == 0)
                                    {
                                        foundSection = true;
                                        continue;
                                    }
                                    else
                                    {
                                        if (foundSection == true)
                                        {
                                            break;
                                        }
                                        foundSection = false;
                                    }
                                }
                            }
                            if (foundSection == true)
                            {
                                string[] pair = line.Split('=');
                                if (pair.Length > 1)
                                {
                                    myAL.Add(pair[0]);
                                }
                            }
                        }
                    }
                }
            }
            return myAL;
        }
    }
}

                        

Hiç yorum yok:

Yorum Gönder