Using the class

Use IniWriteValue
to write a new value to a specific key in a section
or use IniReadValue
to read a value FROM a key in a specific Section.

That's all. It's very easy in C# to include API functions in your
class(es).

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.

No I did'n get any error, If I put the file for Ex. D:\abc.ini then it is easily writable,but I want to do this in C:\Windows folder.when I execute code with C:\Windows\abc.ini file then nothing is happen means not any error occur, but file also not updated.

Hello Sir,I used this code in my application, Thank for this.I have one doubt WritePrivateProfileString always returns 4294967297 whether it write value successfully or not.so I want to ask that is there any way to know that WritePrivateProfileString() writing values or not.I want to show a message if value is not written.

Your stack trace will now have everything you need, and you can get rid of the messy try...catch...throw. There's no need to include an InnerException as part of the newly thrown IniFileException, as no other Exception has been generated yet.

You have room for a default value now, and you're using overloads instead of multiple alternately named, similar functions. This is a more standardized method for this sort of endeavor.

An int.TryParse is just a hair slower than a Convert.ToInt32 but the overhead this method will save you on failure is well worth it. Additionally, it's the preferred method for conversion.

Hi md5sum, thx for your insight here! Allthough the methodes I posted do what they supposed to, I agreewith overloading, the methods you posted.I made changes accordingly. However I am not sureabout using tryparse. if reading from ini would fail becausethe number is too high to be parsed or there is an invalidformat, then you can't distinqush between those cases from iniexception anymore.

in order to introduce the same level of reporting the cause of the error without the granularity of exception types generated through the alternate, multiple catch method. I can't imagine an instance where I would need (for myself or my users) to have more than a single exception generated from this block of code. In general though, if you absolutely want to keep the InnerException and specifically NOT use a TryParse, I would limit it to simply:

file is beeter than stream read and stream write because stream write is used once if we used more than once it overwrite thats file is beeter option because it does not overwrite and there is no use of close0, flush in file .