Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  Directory functions in C#
Directory functions in C#
by John Spano | Published  11/27/2002 | .NET Framework | Rating:
John Spano

John Spano cofounder and CTO of NeoTekSystems, a Greenville, South Carolina technology consulting company. NeoTekSystems offers IT consulting, custom programming, web design and web hosting. We specialize in Microsoft .Net enterprise development and business design.

I have six years of experience in software architecture. My primary focus is on Microsoft technologies, and I have been involved in .NET since beta 1. I currently hold a MCSD certification, 2 MCTS's (Windows, Web) a MCPD in Distributed, 2 MCITP's, a Microsoft MVP, and have won the Helper of the Month contest for July 2002 in the devCity.NET forums.

Corporate URL:
Primary email:
Alternate email:


View all articles by John Spano...
Directory functions in C#

Here are some useful file managing functions in .NET

The use the System.IO namespace
All functions also assume the logged in user has the security needed to delete the file or directory, if not they will throw an error of SecurityException

To create a directory



To move a directory

Directory.Move(@"C:\MyNewDir", @"C:\MyMovedDir");

To delete a directory


To Delete a directory recursively

Directory.Delete(@"C:\MyNewDir", true);

To Delete a File


To Move a File

File.Move(@"C:\MyFile.Txt", @"C:\MyOtherDir\MyFile.Txt");

To Copy a file

File.Copy(@"C:\MyFile.Txt", @"C:\MyOtherDir\MyFile.Txt");
//To copy to a different file name is also possible
File.Copy(@"C:\MyFile.Txt", @"C:\MyOtherDir\MyNewFileName.Txt");

To get information about a file, like the length
You can also get the extension, directory, LastAccessedtime, LastModifiedTime, wether the file exists or not, the creation date, attributes of the file etc, from the FileInfo class

FileInfo FI = new FileInfo(@"C:\MyFile.Txt");
Console.WriteLine("File size of MyFile.Txt: {0}", FI.Length);

This article was originally posted as devCity.NET Forums FAQ -

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:3.91946308724833 out of 5
 149 people have rated this page
Article Score85839
Sponsored Links