Reputation: 27
I have some ini
file include this string : _DeviceName = #1234
. Now I want to get the _DeviceName
value that is 1234
but it shows me the full string that is _DeviceName = #1234
.
I tried this code:
if (File.Exists("inifile.ini"))
{
if (File.ReadAllText("inifile.ini").Split('\r', '\n').First(st => st.StartsWith("_DeviceName")) != null)
{
string s = File.ReadAllText("inifile.ini").Split('\r', '\n').First(st => st.StartsWith("_DeviceName"));
MessageBox.Show(s);
}
}
Upvotes: 1
Views: 1637
Reputation: 26
I made an application that implements an INI Reader/Writer. Source code not available just yet, but keep checking back, I will have the source uploaded in a few days ( today is 8/14/13) . This particular INI Reader is CASE SENSITIVE and may need modification for special characters, but so far I have had no problems.
http://sourceforge.net/projects/dungeonchest/
Upvotes: 0
Reputation: 28737
You could add another split to get the value out:
if (File.Exists("inifile.ini"))
{
if (File.ReadAllText("inifile.ini").Split('\r', '\n').First(st => st.StartsWith("_DeviceName")) != null)
{
string s = File.ReadAllText("inifile.ini")
.Split('\r', '\n').First(st => st.StartsWith("_DeviceName"))
.Split('=')[1];
MessageBox.Show(s);
}
}
Upvotes: 2
Reputation: 69372
You can use File.ReadAllLines instead. You may want to look at existing ini
file readers if you're doing anything more complex but this should work. As a side note, it's not efficient to make two File.ReadAllText
calls so quickly; in most cases it's best to just store the result in a variable).
if (File.Exists("inifile.ini"))
{
string[] allLines = File.ReadAllLines("inifile.ini");
string deviceLine = allLines.Where(st => st.StartsWith("_DeviceName")).FirstOrDefault();
if(!String.IsNullOrEmpty(deviceLine))
{
string value = deviceLine.Split('=')[1].Trim();
MessageBox.Show(value);
}
}
Upvotes: 2