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.

0

1 Answer 1

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);
9
  • but it has to be read from the txt file. That is why i used 'ReadFile.ReadLine();'
    – Caleb
    Commented May 27, 2020 at 12:48
  • i am really sorry but i am still getting this error message:
    – Caleb
    Commented May 27, 2020 at 13:26
  • System.FormatException: 'String was not recognized as a valid DateTime.'
    – Caleb
    Commented May 27, 2020 at 13:26
  • Can you post the value of the text you are getting after Regex.Replace?
    – JeremyRock
    Commented May 27, 2020 at 13:47
  • if i don't store data, i don't get any value.
    – Caleb
    Commented May 27, 2020 at 19:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.