I want to read settings from a text file.
string MySettingsFile = "Settings.txt";
Normally, the file would have 4 lines.
SettingA:Alpha1
SettingB:Bravo2
SettingC:Charlie1
SettingD:Delta6
I want to get each line in it's own variable, like this :
string MyAlpa = "Alpha1";
string MyBravo = "Bravo2";
string MyCharlie = "Charlie1";
string MyDelta = "Delta6";
Normally, I would just read the lines in a loop, reading each line and setting the string as I go. If, however, Line 4 is missing, and I am looking for the part of the line after the colon, I get an error if I check for it like this...
MyDelta = MyDeltaSubstring(MyDelta.LastIndexOf(':') + 1);
Is there a way to 'check for the existence of a specific line' before I attempt to get the SubString (so it doesn't get an error), like in a function separate that has try, catch, finally with return either the string I want or the word "Missing", if that line is missing (and then stop since there are no more lines)?
function DoesLineExist(int X, string myFile)
{
string MyString;
try ()
{
// read line X from file myFile
// get everything AFTER the ":" and put it in MyString
// ??? //
}
catch (ArgumentException null)
{
MyString = "Missing";
}
catch (ArgumentException e)
{
MyString = "Missing";
}
finally
{
MyString = ? // whatever we got From Line X (after ":")
}
return MyString; // only if line is missing
}
Is there a better way of doing this? ReadAllLines or something maybe?
You need to first verify that that line is exist or not and then again check that is line contains key/value pair of settings is exist and then project your key value pair into dictionary and then get each setting to your variable by its key name.
Here i create a console app for your demonstration purpose.
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> dictSettings = new Dictionary<string, string>();
string MyAlpa = "";
string MyBravo = "";
string MyCharlie = "";
string MyDelta = "";
var lines = File.ReadAllLines(@"C:\Users\xxx\source\repos\ConsoleApp4\ConsoleApp4\Files\Sample.txt");
for (var i = 0; i < lines.Length; i += 1)
{
var line = lines[i];
//DoesLineExist(line);
if (!string.IsNullOrEmpty(line) && line.Contains(":"))
{
string settingKey = line.Split(':')[0];
string settingValue = line.Split(':')[1];
dictSettings.Add(settingKey, settingValue);
}
}
MyAlpa = dictSettings.ContainsKey("SettingA") ? dictSettings["SettingA"] : "";
MyBravo = dictSettings.ContainsKey("SettingB") ? dictSettings["SettingB"] : "";
MyCharlie = dictSettings.ContainsKey("SettingC") ? dictSettings["SettingC"] : "";
MyDelta = dictSettings.ContainsKey("SettingD") ? dictSettings["SettingD"] : "";
Console.WriteLine(MyAlpa);
Console.WriteLine(MyBravo);
Console.WriteLine(MyCharlie);
Console.WriteLine(MyDelta);
Console.ReadLine();
}
//private static void DoesLineExist(string line)
//{
// if (!string.IsNullOrEmpty(line) && line.Contains(":"))
// {
// string settingKey = line.Split(':')[0];
// string settingValue = line.Split(':')[1];
// dictSettings.Add(settingKey, settingValue);
// }
//}
}
Input:
SettingA:Alpha1
SettingB:Bravo2
SettingC:Charlie1
Output:
Input:
SettingA:Alpha1
SettingC:Charlie1
SettingD:
Output: