Jump to content

INI Read/Write


Paul Thomas
 Share

Recommended Posts

I had posted this in the downloads section for anyone who wants to use it: http://leadwerks.com/werkspace/index.php?app=downloads&showfile=58

 

A C++ forum post reminded me of this program I made during C# college classes to read/write to INI files. I provided source and DLL. It most likely will need to be expanded as I only went as far as I needed it for. It simply uses InteropServices to use "WritePrivateProfileString" and "GetPrivateProfileString."

  • Upvote 1
Link to comment
Share on other sites

Here's another C# implementation I made for one of my previous projects:

 

EDIT: Interesting, wonder how you did the "remove keys". Manual parsing or Kernel32?

EDIT2: I'll just download the source and see for myself. The description seems quite amazing.

 

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

public class IniFile
{
public string Path { get; set; }

[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 path, bool forceLoad)
{
	if (!File.Exists(this.Path))
	{
		throw new FileNotFoundException();
	}

	this.Path = path;
}

public void Write(string section, string key, string value)
{
	WritePrivateProfileString(section, key, value, this.Path);
}

public string Read(string section, string key)
{
	StringBuilder sb = new StringBuilder(255);
	GetPrivateProfileString(section, key, "", sb, 255, this.Path);

	string result = sb.ToString();

	if (result == string.Empty)
	{
		throw new Exception("Cannot read field \"" + key + "\" at section \"" + section + "\".");
	}
	else
	{
		return result;
	}
}
}

Link to comment
Share on other sites

Yeah, could just combine the two.

 

ini.Read("category", "key");

 

Looks better than my ini.Key("category", "key");

 

I think back when I was writing this I was going to do ini.Read("category","key") to directly read from a category and make ini.Key("key") to iterate through the ini.Categories() and ini.Keys("category") to find that one key, but obviously dumped that idea.

 

It was a learning experience back then, it could use polishing and expanding.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...