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: www.NeoTekSystems.com
Primary email: JSpano@NeoTekSystems.com
Alternate email: Jspano@devcity.net.

 

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

Code:

Directory.CreateDirectory(@"C:\MyNewDir");


To move a directory

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


To delete a directory

Code:
Directory.Delete(@"C:\MyMovedDir");


To Delete a directory recursively

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


To Delete a File

Code:
File.Delete(@"C:\MyFile.Txt");


To Move a File

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


To Copy a file

Code:
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

Code:
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 - http://www.devcity.net/forums/faq.asp?fid=31#TID4904

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:4.64912280701755 out of 5
 57 people have rated this page
Article Score46379
Related Articles
Comments    Submit Comment

Comment #1  (Posted by an unknown user on 02/23/2005)
Rating
Flat out told you, no extra crap or anythign like that.

Perfect!
 
Comment #2  (Posted by an unknown user on 09/02/2005)
Rating
Succinct!
 
Comment #3  (Posted by an unknown user on 05/15/2006)
Rating
Exactly the info with code-snippets that I was looking for.
 
Comment #4  (Posted by an unknown user on 05/19/2006)
Rating
because hell yes it rox
 
Comment #5  (Posted by an unknown user on 07/27/2006)
Rating
thx
 
Comment #6  (Posted by an unknown user on 08/21/2006)
Rating
Just what I needed, nothing else.
 
Comment #7  (Posted by an unknown user on 09/12/2006)
Rating
straight and to the point, good work
 
Comment #8  (Posted by an unknown user on 10/03/2006)
Rating
EXCELLENT
 
Comment #9  (Posted by an unknown user on 10/13/2006)
Rating
More pages like this !!
 
Comment #10  (Posted by an unknown user on 12/09/2006)
Rating
this is very useful for my project
 
Comment #11  (Posted by an unknown user on 12/09/2006)
Rating
I have been lookin for this page for ages. finally I have successfully managed to write and delete on linux machine.
 
Comment #12  (Posted by an unknown user on 12/14/2006)
Rating
i would have rated this article as a 5 because it had the File.Copy method in it. i bet you will want me to sign up now, won't you. It does not say whether this will work in .Net 2.0 or 1.1 or neither or both! Hence I am not very satisfied!
 
Comment #13  (Posted by an unknown user on 01/07/2007)
Rating
TY! :D
 
Comment #14  (Posted by an unknown user on 01/13/2007)
Rating
It's compact and to the point. Good enough for 5. But I was looking for functions like, to check if a directory or file exists or not.
 
Comment #15  (Posted by an unknown user on 01/26/2007)
Rating
Cut to the point.
 
Comment #16  (Posted by an unknown user on 02/22/2007)
Rating
Showed me most of what I was looking for :)

Thank you.
 
Comment #17  (Posted by an unknown user on 03/01/2007)
Rating
thanks so much :-)
 
Comment #18  (Posted by an unknown user on 03/25/2007)
Rating
man i was just trying to find out how to copy / make directories (i was trying to see if i could make folders)
and this was it
and it wasnt something like msdn where they do like args mega super duper z0mg high 1337 things that noone understands
this just told u what u wanted
nothing else

PERFECT!

thx
 
Comment #19  (Posted by an unknown user on 04/12/2007)
Rating
Spot on.
 
Comment #20  (Posted by an unknown user on 04/21/2007)
Rating
very objective and usefull, thanks.
 
Comment #21  (Posted by an unknown user on 05/11/2007)
Rating
it's very clear
 
Comment #22  (Posted by an unknown user on 06/13/2007)
Rating
To the point
This is what I wanted
Thanks
 
Comment #23  (Posted by an unknown user on 07/02/2007)
Rating
it's simple and straight forward
 
Comment #24  (Posted by an unknown user on 07/06/2007)
Rating
Straight forward and direct on to the center point, no miss !!!
I have no words to thank.
Subbs
 
Comment #25  (Posted by an unknown user on 07/17/2007)
Rating
Right on the mark!!
Thanks for it :)
 
Comment #26  (Posted by Bob Aviles on 08/10/2007)
Rating
Thank you!!!!
 
Comment #27  (Posted by an unknown user on 10/15/2007)
Rating
I thought the page was helpful. I was interested in the Delete file section. What does the @ sign do? Is there a way to have a variable in the file path? Thanks
 
Comment #28  (Posted by an unknown user on 10/23/2007)
Rating
What about copy a directory, and also copy a directory recursively?
 
Comment #29  (Posted by an unknown user on 12/27/2007)
Rating
simple, useful, and quick to the point.
 
Comment #30  (Posted by an unknown user on 01/04/2008)
Rating
Thank You
 
Comment #31  (Posted by an unknown user on 02/08/2008)
Rating
Clean and good example code, very helpfull.
 
Sponsored Links