0% found this document useful (0 votes)
52 views2 pages

How To: Copy Directories: Using Using Class Static Void

This document provides an example of how to recursively copy the contents of a directory to another location using C# and I/O classes. The example method allows the user to specify whether to also copy subdirectories. If copying subdirectories, the method calls itself to recursively copy all subdirectory contents to the new location until there are no more to copy. Key classes used include DirectoryInfo, FileInfo, and Path to get directory and file information and copy files to the destination path while preserving the directory structure.

Uploaded by

anandkumarrs6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views2 pages

How To: Copy Directories: Using Using Class Static Void

This document provides an example of how to recursively copy the contents of a directory to another location using C# and I/O classes. The example method allows the user to specify whether to also copy subdirectories. If copying subdirectories, the method calls itself to recursively copy all subdirectory contents to the new location until there are no more to copy. Key classes used include DirectoryInfo, FileInfo, and Path to get directory and file information and copy files to the destination path while preserving the directory structure.

Uploaded by

anandkumarrs6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

How to: Copy Directories

This example demonstrates how to use I/O classes to synchronously copy the contents of a
directory to another location. In this example, the user can specify whether to also copy the
subdirectories. If the subdirectories are copied, the method in this example recursively
copies them by calling itself on each subsequent subdirectory until there are no more to
copy.
using System;
using System.IO;
class DirectoryCopyExample
{
static void Main()
{
// Copy from the current directory, include subdirectories.
DirectoryCopy(".", @".\temp", true);
}
private static void DirectoryCopy(string sourceDirName, string
destDirName, bool copySubDirs)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
DirectoryInfo[] dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, false);
}

// If copying subdirectories, copy them and their contents to new


location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
}

You might also like