Click here to Skip to main content
13,150,544 members (36,057 online)
Click here to Skip to main content
Add your own
alternative version

Stats

27.2K views
16 bookmarked
Posted 6 Sep 2006

Load config files

, 6 Sep 2006
Rate this:
Please Sign up or sign in to vote.
Load an ini file to configure own application
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->

Introduction

this class is to read ini files

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace touzasClass
{
    public static class config{
        public static string ruta = System.Windows.Forms.Application.StartupPath + @"\config.ini";
        public static IniFile ini = new IniFile(ruta);

        public static string getKeyValue(string section, string key){
            string tmp = String.Empty;
            tmp = ini.IniReadValue(section, key);
            return tmp;
        }
    }

    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);

        public IniFile(string INIPath) {
            path = INIPath;
        }
        public void IniWriteValue(string Section,string Key,string Value) {
            WritePrivateProfileString(Section,Key,Value,this.path);
        }
        
        public string IniReadValue(string Section,string Key) {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section,Key,"",temp, 255, this.path);
            return temp.ToString();

        }
    }
}
=============================================================================================
==================================== SAMPLE  ============================================
=============================================================================================

string server = string.empty;
server = config.getKeyValue("Configuracion", "SERVIDOR")
--- RESULT ---
server = "localhost"

=============================================================================================
==================================== ARCHIVO config.ini ============================================
=============================================================================================

[Configuracion]
SERVIDOR = localhost
DATABASE = dbPruebas
USUARIO = userdb
CLAVE = pwd

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

Javier Lema
Software Developer (Senior)
Spain Spain
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionINI is old stuff Pin
Henize23-Sep-06 19:51
memberHenize23-Sep-06 19:51 
AnswerRe: INI is old stuff Pin
kasparovthe215-Oct-09 1:44
memberkasparovthe215-Oct-09 1:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170924.2 | Last Updated 6 Sep 2006
Article Copyright 2006 by Javier Lema
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid