Reset file access and write times when the user drags files onto an executable program in C#

This handy utility resets the last access and last write times for any files that are dragged onto its executable. (I needed it because I had some files that were created while the system's clock was wrong so they had last access dates one year in the future. That made it hard to find my most recent files in Windows Explorer.)

This program is a console application and doesn't display a user interface at run time.

Aside:

To create a console application, start a new application, selecting the Console Application template. Then enter whatever code you want to execute in the Main method that is created for you.

Note that a console application ends as soon as the Main method exits. Often the program uses Console.WriteLine and Console.ReadLine to display output and get input from the user. It is very common for programmers to forget to put any kind or ReadLine statement in the code so the Main method executes, finishes, and the program ends before the user can see anything.

The following code shows how this program works.

static void Main(string[] args)
{
// Update each file's last access time.
foreach (string filename in args)
{
File.SetLastAccessTime(filename, DateTime.Now);
File.SetLastWriteTime(filename, DateTime.Now);
}
}

The parameter to the Main method is an array of strings holding the program's command line parameters. If you were to run the program from a command shell, this would list the values that you placed after the program's name when you invoked it. When you drag and drop files onto an executable, the args parameter lists the names of those files.

This program simply loops through the files, resetting their last modified and last access dates to be the current date.

  

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.