Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    10
  • comments
    16
  • views
    11040

Cheap.

Sign in to follow this  
Reddox

542 views

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.
Sign in to follow this  


3 Comments


Recommended Comments

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!

Share this comment


Link to comment
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 :)

Share this comment


Link to comment
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.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!