-1

---EDITED---

I have a simple C# Windows Form. "c:\date.txt" is just (DateTime.Now). Later I will create some code to update this file, but for now, I'm having a problem with debugging. It builds fine, but debug throws an exception at the date.txt file... The code is as follows:

public partial class Form1 : Form
{
    private void timer1_Tick(object sender, EventArgs e)
    {
        DateTime dt = DateTime.Parse(@"C:\date.txt"); // Exception Thrown Here
                      // Should be: (File.ReadAllText(@"C:\date.txt"))
        DateTime dn = DateTime.Now;
        TimeSpan dc = (dn - dt);
        label1.Text = ("Days = " + dc.Days + 
                    "\nHours = " + dc.Hours +
                    "\nMins = " + dc.Minutes +
                    "\nSecs = " + dc.Seconds);
    }
}

FormatException was unhandled... The string was not recognized as a valid DateTime. There is an unknown word starting at index 0. "When converting a string to DateTime, parse the string to take the date before putting each variable into the DateTime object."

I am parsing! I've tried (new StreamReader...ToString()). I've tried Convert.ToDateTime(...). I've tried making it a method... I've tried TryParse. Nothing is helping. What do I need to do here to make this thing read the string from the file?

Fixer
  • 23
  • 1
  • 8
  • 4
    maybe you should first read out the lines of text in the file and parse those instead of trying to parse the filepath – grabthefish Jan 01 '15 at 20:03
  • 2
    Well, you need to pass a string that represents a date to `DateTime.Parse`, not a file name – Rufus L Jan 01 '15 at 20:04
  • You might want to refer this as an example: http://www.dotnetperls.com/datetime-parse DateTime.Parse expects a datetime string as input and not a file name. – Rush Jan 01 '15 at 20:05
  • Yup. DateTime is trying to parse a string that isn't there. --> string dateThen = File.ReadAllText(@"c:\date.txt"); <-- Now DateTime can parse a string. Duh. I'm just learning and have been pouring over MSDN and StackOverflow for days. Thanks, Guys. – Fixer Jan 02 '15 at 20:26

3 Answers3

3

If the file only contains a single line that contains a date, you can do this:

DateTime dt = DateTime.Parse(File.ReadAllText(@"C:\date.txt"));
Rufus L
  • 36,127
  • 5
  • 30
  • 43
1

Of course, you are trying to parse the string "C:\date.txt" to a DateTime. You better try this:

System.IO.StreamReader file = new System.IO.StreamReader(@"c:\date.txt");
string txt = file.ReadLine();
DateTime dt = DateTime.Parse(txt);
file.Close();
dario
  • 5,149
  • 12
  • 28
  • 32
0

Thank you everyone. These were great answers. YES - DateTime.Parse is looking to parse a "string", not a file or a file.ToString()...

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        if (!File.Exists(@"C:\tmp\date.txt"))
        {
            File.WriteAllText(@"C:\tmp\date.txt", DateTime.Now.ToString());
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        DateTime dn = DateTime.Now;
        DateTime dt = DateTime.Parse(File.ReadAllText(@"C:\tmp\date.txt"));
                          // Read the Text ^      From the File ^
        TimeSpan dc = dn - dt;
        label1.Text = "Now: " + dn.ToString() +
                    "\nThen: " + dt.ToString() +
                    "\nDifference..." +
                    "\nDays: " + dc.Days.ToString() +
                    "\nHours: " + dc.Hours.ToString() +
                    "\nMins: " + dc.Minutes.ToString() +
                    "\nSecs: " + dc.Seconds.ToString();
    }
}

This was part of a date/time counter that counts up from the last time an occurrence happened at work. A lot of people stop and ask questions about the display and about the issue that sparked the last reset of the counter.

Fixer
  • 23
  • 1
  • 8