0

I was surprised that C# doesn't support ini handle but of course i knew that it wouldn't be so hard to do so a simple search gave me this small and easy class to handle simple ini files http://www.codeproject.com/Articles/1966/An-INI-file-handling-class-using-C

iniReadValue returns a string and i want to convert it to integer. I though that a Convert.ToInt32 would work but it doesn so this wont work

int keyclosewindows = Convert.ToInt32(ini.IniReadValue("Key info", "CloseWindows"));

The ini read returns me an integer stored in a string

ini.IniReadValue("Key info", "CloseWindows") //= "1"

I know this is a silly question but unfortunately i can not come up with a solution

Error message:

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: Input string was not in a correct format.
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user2530266
  • 287
  • 3
  • 18
  • 1
    @L.B Will give the same issue as Convert.ToInt32 – Reed Copsey Jul 09 '14 at 23:06
  • gives same issue as Convert.ToInt32 – user2530266 Jul 09 '14 at 23:07
  • @L.B I suspect this is actually not the same as the other question, in that I'm guessing that the string includes `"` here (which is why it's failing). The OP already demonstrated knowledge of how to do a conversion, but it's not working *in this case*. – Reed Copsey Jul 09 '14 at 23:08
  • Can you tell us exactly what is the error message? – Steve Jul 09 '14 at 23:08
  • This is the error: An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Input string was not in a correct format. i will edit 1st post – user2530266 Jul 09 '14 at 23:12
  • 2
    So the value read from your file is not a valid integer value. At this point probably the answer below is pointing to the right direction. You have some character that cannot be parsed as an integer – Steve Jul 09 '14 at 23:14
  • FYI, .ini files are rarely used these days - we use XML instead. – John Saunders Jul 09 '14 at 23:15
  • I am using ini to be able to allow users to makes easy changes... @Steve the value is "1". It happens the same if i set hexes. If the value is "0x31" it will throw me the same error – user2530266 Jul 09 '14 at 23:18
  • 1
    I have downloaded the code from your link and created a simple INI file with the values used by you. (No quotes around the 1) and it works perfectly. Perhaps you could paste also that part of the INI file. – Steve Jul 09 '14 at 23:24
  • No way to guess what the problem is without data - please show value of `ini.IniReadValue("Key info", "CloseWindows")` call. – Alexei Levenkov Jul 09 '14 at 23:27
  • I could reproduce the error if I mispell the the Section value. In that case no section is found and the return is an empty string that cannot be converted – Steve Jul 09 '14 at 23:28
  • In case you or anyone else reading this is interested, I've built an small library for parsing ini files easily: https://github.com/rickyah/ini-parser is open source (MIT) and written completely in c# – Ricardo Amores Dec 07 '14 at 19:44

1 Answers1

2

If the result of IniReadValue includes the quotation marks, you'll need to trim them off before converting to an integer:

var temp = ini.IniReadValue("Key info", "CloseWindows").Trim('\"');
int keyclosewindows = Convert.ToInt32(temp);

Otherwise, the issue is that the ini key either doesn't exist, or is in a format that isn't an integer.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • +1 "includes the parentheses" you probably mean quotes, but does not change answer - trim extra characters before parsing. – Alexei Levenkov Jul 09 '14 at 23:28
  • It seems that the quotes are automatically removed by that library (or peraphs by the API itself) – Steve Jul 09 '14 at 23:30