9.3 Copy, Move, or Delete a File or a Directory


Problem

You need to copy, move, or delete a file or directory.

Solution

Create a System.IO.FileInfo object for a file or a System.IO.DirectoryInfo object for a directory, supplying the path in the constructor. You can then use the object's methods to copy, move, and delete.

Discussion

The FileInfo and DirectoryInfo classes include a host of valuable methods for manipulating files and directories. Table 9.2 shows methods for the FileInfo class, while Table 9.3 shows methods for the DirectoryInfo class.

Table 9.2: Methods for Manipulating a FileInfo Object

Member

Description

CopyTo

Copies a file to the new path and file name specified as a parameter. It also returns a new FileInfo object that represents the new ( copied ) file. You can supply an optional additional parameter of true to allow overwriting.

Create and CreateText

Create creates the specified file and returns a FileStream object that you can use to write to it. CreateText performs the same task, but returns a StreamWriter object that wraps the stream. For more information about writing files, see recipes 9.7 and 9.8.

Open , OpenRead , OpenText , and OpenWrite

Open a file (provided it exists). OpenRead and OpenText open a file in read-only mode, returning a FileStream or a StreamReader object. OpenWrite opens a file in write-only mode, returning a FileStream . For more information about reading files, see recipes 9.7 and 9.8.

Delete

Removes the file, if it exists.

MoveTo

Moves the file to the new path and file name specified as a parameter. MoveTo can also be used to rename a file without changing its location.

Table 9.3: Methods for Manipulating a DirectoryInfo Object

Member

Description

Create

Creates the specified directory. If the path specifies multiple directories that don't exist, they will all be created at once.

CreateSubdirectory

Creates a directory with the specified name in the directory represented by the DirectoryInfo object. It also returns a new DirectoryInfo object that represents the subdirectory.

Delete

Removes the directory, if it exists. If you want to delete a directory that contains other directories, you must use the overloaded Delete method that accepts a parameter named recursive and set it to true .

MoveTo

Moves the directory (contents and all) to a new path. MoveTo can also be used to rename a directory without changing its location.

One useful feature that's missing from the DirectoryInfo class is a copy method. Fortunately, you can write this logic easily enough by relying on recursive logic and the FileInfo object. Here's a helper function that can copy any directory, and its contents:

 using System; using System.IO; public class FileSystemUtil {     public static void CopyDirectory(DirectoryInfo source,       DirectoryInfo destination) {         if (!destination.Exists) {             destination.Create();         }         // Copy all files.         FileInfo[] files = source.GetFiles();         foreach (FileInfo file in files) {             file.CopyTo(Path.Combine(destination.FullName, file.Name));         }         // Process sub-directories.         DirectoryInfo[] dirs = sourceDir.GetDirectories();         foreach (DirectoryInfo dir in dirs) {             // Get destination directory.             string destinationDir = Path.Combine(destination.FullName,               dir.Name);             // Call CopyDirectory() recursively.             CopyDirectory(dir, new DirectoryInfo(destinationDir));         }     } } 

Here's a simple test application that copies directories based on the supplied command-line arguments:

 public class CopyDir {     private static void Main(string[] args) {         if (args.Length != 2) {             Console.WriteLine("usage:  " +                "CopyDir [sourcePath] [destinationPath]");             return;         }         DirectoryInfo sourceDir = new DirectoryInfo(args[0]);         DirectoryInfo destinationDir = new DirectoryInfo(args[1]);         FileSystemUtil.CopyDirectory(new DirectoryInfo(sourceDir),           new DirectoryInfo(destinationDir));         Console.WriteLine("Copy complete.");         Console.ReadLine();     } } 



C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net