Click here to Skip to main content
Licence 
First Posted 6 Sep 2006
Views 19,026
Bookmarked 16 times

Load config files

Load an ini file to configure own application
3 votes, 50.0%
1
1 vote, 16.7%
2
1 vote, 16.7%
3

4
1 vote, 16.7%
5
2.21/5 - 6 votes
μ 2.21, σa 2.80 [?]

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Javier Lema

Software Developer (Senior)

Spain Spain

Member
Hi, My name is Javier Lema, i work in galicia (spain) as c#, php analist.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionINI is old stuff PinmemberHenize20:51 23 Sep '06  
AnswerRe: INI is old stuff Pinmemberkasparovthe22:44 15 Oct '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120210.1 | Last Updated 6 Sep 2006
Article Copyright 2006 by Javier Lema
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid