

opened by another (System.IO.IOException e) handle the case of the file already being Delete a file by using File class static Use a try block to catch IOExceptions, to C:\Users\Public\DeleteTest\SubDir\test2.txt To run this sample, create the following files on your drive: Simple synchronous file deletion operations with no user interface. path strings, use the System.IO.Path following example shows how to delete files and directories. System.IO.File.Move(sourceFile, destinationFile) String sourceFile = destinationFile = To move a file or folder to a new location: Simple synchronous file move operations with no user interface. The following example shows how to move files and directories. Keep console window open in debug mode.Ĭonsole.WriteLine("Press any key to exit.") Use static Path methods to extract only the file name from the path.įileName = System.IO.Path.GetFileName(s) ĭestFile = System.IO.Path.Combine(targetPath, fileName) Ĭonsole.WriteLine("Source path does not exist!") Copy the files and overwrite destination files if they already exist. String files = System.IO.Directory.GetFiles(sourcePath) If (System.IO.Directory.Exists(sourcePath)) Note: Check for target path was performed previously "How to: Iterate Through a Directory Tree.") all subfolders under the current directory, see To copy all the files in one directory to another directory. System.IO.File.Copy(sourceFile, destFile, true) overwrite the destination file if it already exists. To copy a file to another location and System.IO.Directory.CreateDirectory(targetPath) If the directory already exists, this method does not create a new directory. To copy a folder's contents to a new location:

String destFile = System.IO.Path.Combine(targetPath, fileName) String sourceFile = System.IO.Path.Combine(sourcePath, fileName) String sourcePath = targetPath = Use Path class to manipulate file and directory paths. C:\Users\Public\TestFolder\SubDir\test.txt To run this sample, first create the following directories and files: Simple synchronous file copy operations with no user interface. The following example shows how to copy files and directories.

For information about how to perform these file operations asynchronously, see Asynchronous File I/O. Another approach is to use platform invoke to call the relevant file-related methods in the Windows Shell.

Use System.IO.FileSystemWatcher to provide events that will enable you to calculate the progress when operating on multiple files. If you want to provide a standard progress dialog box, see How to provide a progress dialog box for file operations. These examples do not provide a progress bar or any other user interface. The following examples show how to copy, move, and delete files and folders in a synchronous manner by using the System.IO.File, System.IO.Directory, System.IO.FileInfo, and System.IO.DirectoryInfo classes from the System.IO namespace.
