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