Attempting to delete files older than x days vb.net

I have a base bit of code that I use to delete text files in a given directory:

For Each file As IO.FileInfo In New IO.DirectoryInfo(filePath).GetFiles("*.txt")
    If (Now - file.CreationTime).Days > intdays Then file.Delete()
    Next

filePath is the directory where the files are located.

intdays is a variable that determines how many days the files should be saved for.

To test the i code, set intdays to 0, assuming it will delete any files in directoy. However, this is not so, but does not create errors.

The time is "Now" # 2/8/2012 13:59:00 PM #, which is greater than 0. But I'm confused, why doesn’t it delete the file?

+5
source share
3 answers

The difference between dates is less than a day .

Days, - 0 , , .

+3

        Dim d = New Date(2012, 11, 21, 16, 0, 0)
        Dim dGreater = New Date(2012, 11, 21, 17, 0, 0)
        Dim result = dGreater - d
        Console.WriteLine(result.Days)

, , Days 0, .. .

0

you can use LastWriteTimeinstead CreateTime, depending on what you are trying to achieve.

0
source

All Articles