top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How can we delete oldest file present in a folder in visual basic

+6 votes
406 views
How can we delete oldest file present in a folder in visual basic
posted Dec 16, 2013 by Neeraj Pandey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

Find the oldest file in something in the following way (change the directory name accordingly)

var oldestFile = FastDirectoryEnumerator.EnumerateFiles(@"c:\windows\system32")
        .OrderBy(f => f.CreationTime).First();

or

var oldestFile2 = new DirectoryInfo(@"c:\windows\system32").GetFiles()
    .OrderBy(f => f.CreationTime).First();

Now delete the file using the method Amit suggested i.e. DeleteFile (you may need to add directory name to make it absolute path)

answer Jan 26, 2014 by Sheetal Chauhan
–1 vote

Use the DeleteFile method to delete the file. The following code demonstrates how to delete the file named test.txt.

My.Computer.FileSystem.DeleteFile("C:\test.txt")

answer Jan 26, 2014 by Amit Kumar Pandey
I think question is about deleting oldest file in the directory...
...