I'm fetching string from output file which will always be either Ok or Err.
After that I'm casting this result Ok or Err to Enum property, which is ok, everything works, but I'm sure that there must be a better way than mine.
Since I'm fetching 3 characters in case that Ok is fetched I need to remove third element from Ok; result.
string message = File.ReadAllText(@"C:\Temp\SomeReport.txt").Substring(411, 3);
if (message == "Ok;") // `;` character should be removed in case that Ok is fetched
{
message = "Ok";
}
Thanks
You could just use String.Trim() to remove the ';' if its there.
string message = File.ReadAllText(@"C:\Temp\SomeReport.txt").Substring(411, 3).TrimEnd(';')
Result:
"Err" = "Err"
"Ok;" = "Ok"
You can just do this:
switch (message)
{
case "Err":
SomeProperty = EnumName.Err;
break;
case "Ok;":
SomeProperty = EnumName.Ok;
break;
default:
throw new Exception("Unexpected file contents: " + message);
}
If you don't like that, you can use TryParse after trimming the semicolon:
EnumName result;
if (Enum.TryParse(message.TrimEnd(';'), out result))
SomePropery = result;
else
throw new Exception("Unexpected file contents: " + message);
Enum message = Enum.Err;
if (Regex.Match(File.ReadAllText(@"C:\Temp\SomeReport.txt"), "(ok.+?){3}", RegexOptions.Singleline).Success)
{
message = Enum.OK;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With