Cheap.

Published April 09, 2008
Advertisement
Sometimes, it pisses me off when compiler/suite developers decide to cheap out on some of their components, yet sell those very same components for $$$ to customers. Talking about a huge profit!

For example, I spent the last 30 minutes customizing a customer made C# .NET class that should of been part of .NET framework. After some googling, I have found the following:

http://www.codeproject.com/KB/cs/cs_ini.aspx

It fit my needs, almost. So I ended up modifying it to make it a bit more full proof and integrated ;) ... at least for C# 2008.


Below is the code ...

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()        {            throw new System.ArgumentException("Parameter cannot be null.", "IniFile");        }        public IniFile(string INIPath)        {            path = INIPath;        }        public void SetFile(string INIPath)        {            path = INIPath;        }        public void WriteValue(string Section, string Key, string Value)        {            WritePrivateProfileString(Section, Key, Value, this.path);        }        private string ReadValue(string Section, string Key)        {            StringBuilder temp = new StringBuilder(255);            int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);            return temp.ToString();        }        private string ReadValue(string Section, string Key, string Def)        {            string sTemp = ReadValue(Section, Key);            if (sTemp == string.Empty)                sTemp = Def;            return sTemp;        }                public string ReadString(string Section, string Key)        {            return ReadValue(Section, Key);        }        public string ReadString(string Section, string Key, string Def)        {            return ReadValue(Section, Key, Def);        }        public int ReadInt(string Section, string Key)        {            int ret;            string r = ReadValue(Section, Key, "0");            int.TryParse(r, out ret);            return ret;        }        public int ReadInt(string Section, string Key, int Def)        {            int ret;            string r = ReadValue(Section, Key, Def.ToString());            int.TryParse(r, out ret);            return ret;        }      }


I am sure there may be other, better ways of doing the above and support all primary data types .net supports. But the above supports my needs nicely.

Sometimes it makes me thankful that the Pure Basic author have at least partially implemented such functionality. That still doesn't make up for lack of support.
Previous Entry All Hail Microsoft!
0 likes 3 comments

Comments

benryves
Why not just use the built-in .NET settings system? It supports all the common .NET data types, and handles saving the settings to the correct directories for you (and you can access the settings directly in your code via properties). Makes life nice and easy!
April 10, 2008 08:02 AM
Reddox
Ah! Thank you for the reply. I was looking at that .. the application.settings file right? I wanted to be able to change settings on the fly w/o recompilation, and did not have time to test.

Thanks for bringing that up, I could look into it a bit more :)
April 10, 2008 12:58 PM
benryves
Quote:I wanted to be able to change settings on the fly w/o recompilation, and did not have time to test.
It's rather more verbose than simple INI files, I'll concede (the saved .config file is XML) and you need to distribute an extra file alongside your program, but I find it's worth it for its ease of use.
April 10, 2008 01:12 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

Sold for good

1474 views

Mirage.NET

1315 views

Cheap.

1030 views

All Hail Microsoft!

1246 views

Of Mirage

1062 views
Advertisement