p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   C# (http://p2p.wrox.com/forumdisplay.php?f=22)
-   -   Reading an INI file in C# (http://p2p.wrox.com/showthread.php?t=57435)

@shish May 3rd, 2007 02:42 PM

Reading an INI file in C#
 
hi,
 I have this problem where i have put in data in a INI file using key value pairs....
eg.
  [section]
  userName=Mr A
  Email=https://www<number>.<URL>.com
  number=8,9
...

the problem that i am facing is that i want to use <number> as a key and assign a value ..could either be 8 or 9...from the C# code that i am using
...i have used [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);

in my C# code to access the data present.. i can access data using the regular key value pairs.. ie the userName by passing the key but simply cannot assign a value to the <number>...(which upto some extent i know must be used as a key) part of the URI.......
please could some body help me with this problem[8D]

achhetri

DeborahStoddard April 1st, 2015 04:00 AM

RE: Reading an INI file in C#
 
I would recommend you to check out this C# library for advance processing of INI files.
It can enable you to bind your data in simple manner, you would need to define a placeholders like this: @{placeholder}

So your INI file's content would be this:

[section]
userName=Mr A
Email=https://www@{number}.@{URL}.com
number=8


And you can execute a binding like the following:

Code:

IniFile file = new IniFile();
file.Load("path-to-ini-file.ini");

// This will bind placeholders with internal data.
// In other words 'number' placeholder will get 'number' key's value.
file.ValueBinding.Bind();

// This will bind placeholders with external data.
// In other words 'URL' placeholder will get KeyValuePair's value.
file.ValueBinding.Bind(
    new KeyValuePair<string, string>("URL", "example"));

// The 'email' variable's value is: https://www8.example.com
string email = file.Sections["section"].Keys["Email"].Value;



All times are GMT -4. The time now is 08:46 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.