0
StreamReader ReadFile;
            ReadFile = File.OpenText("..\\..\\WeatherForecasts.txt");
            string line = ReadFile.ReadLine();
            if (String.IsNullOrEmpty(line) == false)
            {
                txtCity.Text = Regex.Replace(ReadFile.ReadLine(), "City: ", String.Empty);
                dateTimePicker1.Value = DateTime.Parse(Regex.Replace(ReadFile.ReadLine(), "Date: ", String.Empty));
                MinT.Value = decimal.Parse(Regex.Replace(ReadFile.ReadLine(), "Minimum temperature: ", String.Empty));
                MaxT.Value = decimal.Parse(Regex.Replace(ReadFile.ReadLine(), "Maximum temperature: ", String.Empty));
                Prec.Value = decimal.Parse(Regex.Replace(ReadFile.ReadLine(), "Precipitation: ", String.Empty));
                Humidity.Value = decimal.Parse(Regex.Replace(ReadFile.ReadLine(), "Humidity: ", String.Empty));
                WindSpeed.Value = decimal.Parse(Regex.Replace(ReadFile.ReadLine(), "Wind Speed: ", String.Empty));
            }


            ReadFile.Close();

actually this is working but if any data is stored in WeatherForecasts.txt

I get this error message:

System.FormatException: 'The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.'

so I changed like

                    Regex.Replace(ReadFile.ReadLine(), "City: ", String.Empty);
                    Regex.Replace(ReadFile.ReadLine(), "Date: ", String.Empty);
                    Regex.Replace(ReadFile.ReadLine(), "Minimum temperature: ", String.Empty);
                    Regex.Replace(ReadFile.ReadLine(), "Maximum temperature: ", String.Empty);
                    Regex.Replace(ReadFile.ReadLine(), "Precipitation: ", String.Empty);
                    Regex.Replace(ReadFile.ReadLine(), "Humidity: ", String.Empty);
                    Regex.Replace(ReadFile.ReadLine(), "Wind Speed: ", String.Empty);

and I got a different error

System.ArgumentNullException: 'Value cannot be null.
Parameter name: input'

if i don't store data, it works perfectly.

and this is what data is stored in WeatherForecasts.txt now

City: dddd
Date: 5/27/2020 12:00:00 AM
Minimum temperature: 1
Maximum temperature: 1
Precipitation: -1
Humidity: -2
Wind Speed: 1

sorry, I don't know what you need, so if you need any other information, please tell me.

JeremyRock
  • 396
  • 1
  • 8
Caleb
  • 7
  • 1
  • 4

1 Answers1

0

The exception you are getting is related to the Date parsing. By default, Date.Parse uses the computer local settings to parse the date, but it probably does not match the date you are providing (US regional format). Use the Date.ParseExact with the following format:

dateTimePicker1.Value = DateTime.ParseExact(Regex.Replace(ReadFile.ReadLine(), "Date: ", String.Empty),"M/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
JeremyRock
  • 396
  • 1
  • 8
  • but it has to be read from the txt file. That is why i used 'ReadFile.ReadLine();' – Caleb May 27 '20 at 12:48
  • i am really sorry but i am still getting this error message: – Caleb May 27 '20 at 13:26
  • System.FormatException: 'String was not recognized as a valid DateTime.' – Caleb May 27 '20 at 13:26
  • Can you post the value of the text you are getting after Regex.Replace? – JeremyRock May 27 '20 at 13:47
  • if i don't store data, i don't get any value. – Caleb May 27 '20 at 19:57
  • Why store data? Try to see what value you are getting on this call Regex.Replace(ReadFile.ReadLine(), "Date: ", String.Empty). – JeremyRock May 27 '20 at 20:01
  • i mean how can i check the value if i get the error message? sorry i am newbie – Caleb May 27 '20 at 20:31
  • The error is coming from Date.Parse function.Try to break down the function calls: var s = ReadFile.ReadLine() ; var afterReg = Regex.Replace(s, "Date: ", String.Empty); See the value of s and afterReg. – JeremyRock May 27 '20 at 20:37
  • oh my goodness, now i am getting new error for others. Do i need to break down the rest of them as well like that? for example, var MinT = Regex.Replace(s, "Minimum Temperature: ", String.Empty) – Caleb May 27 '20 at 21:05