I'm trying to code a simple module to give me the difference between current date and the age of an existing file in a particular directory. If the file is a certain age (specifed in a config file), then an email is sent. I'm using TimeSpan to give me this magic number but I'm getting the following error:

Value of type 'Date' cannot be converted to 'System.TimeSpan'.

Here is my code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dirInfo As DirectoryInfo
        dirInfo = New DirectoryInfo("c:\Test")

        Dim age As DateTime = ConfigurationSettings.AppSettings("FileTime")

        Dim ageTime As TimeSpan
        Dim timeVal As Integer
        ageTime = Now.Add(age)

        Dim thisFile As FileInfo

        Dim rgFiles As FileInfo()
        rgFiles = dirInfo.GetFiles("*.LCK")

        MsgBox("Age factor: " & age.ToString)
        'MsgBox("Age TimeSpan: " & ageTime.ToString)
        For Each thisFile In rgFiles
            'rgFiles(0).CreationTime.ToString()

            Label1.Text = ("Current Date: " & DateTime.Now.ToString())
            Label2.Text = ("CreationDate: " & thisFile.CreationTime.ToString())
            If ((DateTime.Now - thisFile.CreationTime) > ageTime) Then
                ListBox1.Items.Add(thisFile.Name & " " & thisFile.CreationTime)
                MsgBox("File Age: " & thisFile.Name & " - " & (DateTime.Now - thisFile.CreationTime).ToString())
            End If

        Next

    End Sub

Argument type mismatch Now.Add(age). Take a look at following code snippet.

Dim age As DateTime = #12/5/2008#

        Dim ageTime As TimeSpan
        ageTime = Now - age

        Dim diff As Date = Date.MinValue + ageTime
This article has been dead for over six months. Start a new discussion instead.