[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Easily reset file access and write times in C#

[Easily reset file access and write times in C#]

You can use this handy utility to reset 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 so any input and output appears in the Console window.

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 to 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.

There's still one catch. Even though the program doesn't use its Console window, the window still appears briefly. You can prevent that by double-clicking Properties in Solution Explorer and setting the program's "Output type" to Windows Application. Now the program expects to be visible in windows but it doesn't create any windows.

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 args parameter passed into 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 the files you dragged.

This program simply loops through the files to reset each file's last modified and last access dates to be the current date. Then the Main method ends and the program exits without ever displaying anything to the user.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.