Tải bản đầy đủ - 0 (trang)
5-3. Copy, Move, or Delete a File or Directory

5-3. Copy, Move, or Delete a File or Directory

Tải bản đầy đủ - 0trang

CHAPTER 5 ■ FILES, DIRECTORIES, AND I/O



Method



Description



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.



Replace



Replaces contents of a file by the current FileInfo object. This method could also

take a backup copy of the replaced file.



Table 5-3. Key Methods for Manipulating a DirectoryInfo Object



Method



Description



Create



Creates the specified directory. If the path specifies multiple directories that do

not 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 on the same drive. MoveTo

can also be used to rename a directory without changing its location.



The Code

One useful feature that is 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.

The following example contains a helper function that can copy any directory, and its contents.

using System;

using System.IO;

namespace Apress.VisualCSharpRecipes.Chapter05

{

static class Recipe05_03

{

static void Main(string[] args)

{

if (args.Length != 2)

{



214



www.it-ebooks.info



CHAPTER 5 ■ FILES, DIRECTORIES, AND I/O



Console.WriteLine("USAGE: " +

"Recipe05_03 [sourcePath] [destinationPath]");

Console.ReadLine();

return;

}

DirectoryInfo sourceDir = new DirectoryInfo(args[0]);

DirectoryInfo destinationDir = new DirectoryInfo(args[1]);

CopyDirectory(sourceDir, destinationDir);

// Wait to continue.

Console.WriteLine(Environment.NewLine);

Console.WriteLine("Main method complete. Press Enter.");

Console.ReadLine();

}

static void CopyDirectory(DirectoryInfo source,

DirectoryInfo destination)

{

if (!destination.Exists)

{

destination.Create();

}

// Copy all files.

foreach (FileInfo file in source.EnumerateFiles())

{

file.CopyTo(Path.Combine(destination.FullName,

file.Name));

}

// Process subdirectories.

foreach (DirectoryInfo dir in source.EnumerateDirectories())

{

// Get destination directory.

string destinationDir = Path.Combine(destination.FullName,

dir.Name);

// Call CopyDirectory() recursively.

CopyDirectory(dir, new DirectoryInfo(destinationDir));

}

}

}

}



215



www.it-ebooks.info



CHAPTER 5 ■ FILES, DIRECTORIES, AND I/O



5-4. Calculate the Size of a Directory

Problem

You need to calculate the size of all files contained in a directory (and optionally, its subdirectories).



Solution

Examine all the files in a directory and add together their FileInfo.Length properties. Use recursive logic

to include the size of files in contained subdirectories.



How It Works

The DirectoryInfo class does not provide any property that returns size information. However, you can

easily calculate the size of all files contained in a directory using the FileInfo.Length property.



The Code

The following example calculates the size of a directory and optionally examines contained directories

recursively.

using System;

using System.IO;

namespace Apress.VisualCSharpRecipes.Chapter05

{

static class Recipe05_04

{

static void Main(string[] args)

{

if (args.Length == 0)

{

Console.WriteLine("Please supply a directory path.");

return;

}

DirectoryInfo dir = new DirectoryInfo(args[0]);

Console.WriteLine("Total size: " +

CalculateDirectorySize(dir, true).ToString() +

" bytes.");

// Wait to continue.

Console.WriteLine(Environment.NewLine);

Console.WriteLine("Main method complete. Press Enter.");

Console.ReadLine();

}



216



www.it-ebooks.info



CHAPTER 5 ■ FILES, DIRECTORIES, AND I/O



static long CalculateDirectorySize(DirectoryInfo directory,

bool includeSubdirectories)

{

long totalSize = 0;

// Examine all contained files.

foreach (FileInfo file in directory.EnumerateFiles())

{

totalSize += file.Length;

}

// Examine all contained directories.

if (includeSubdirectories)

{

foreach (DirectoryInfo dir in directory.EnumerateDirectories())

{

totalSize += CalculateDirectorySize(dir, true);

}

}

return totalSize;

}

}

}



5-5. Retrieve Version Information for a File

Problem

You want to retrieve file version information, such as the publisher of a file, its revision number,

associated comments, and so on.



Solution

Use the static GetVersionInfo method of the System.Diagnostics.FileVersionInfo class.



How It Works

The .NET Framework allows you to retrieve file information without resorting to the Windows API.

Instead, you simply need to use the FileVersionInfo class and call the GetVersionInfo method with the

file name as a parameter. You can then retrieve extensive information through the FileVersionInfo

properties.



217



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

5-3. Copy, Move, or Delete a File or Directory

Tải bản đầy đủ ngay(0 tr)

×