Subscribe

RSS Feed (xml)

Copy, Move, or Delete a File or a Directory

The FileInfo and DirectoryInfo classes include a host of valuable methods for manipulating files and directories. Table 1 shows methods for the FileInfo class, while Table 2 shows methods for the DirectoryInfo class.
Table 1: 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.

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.

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 2: 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();
}
}




Technorati :

No comments:

Post a Comment

Archives

LocalsAdda.com-Variety In Web World

Fun Mail - Fun in the Mail