473,320 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

howto recursively copy a folder ?

hi all !!!

I need to copy a folder and its subfolders to another location; which class
should I use ?

could only find the System.IO.Directory.MOVE but don't know how to COPY.

thanks in advance,
ant


Jul 21 '05 #1
1 3290
It does not really exists a class that does this out of the box in .NET (as
far as I know ;-) )
However, here is a sample:

You should replace the BTException class (my custom exception class by
yours...)
José

//--------------------------------------------------------------------------
---------------
/// <summary>
/// This method will make a copy of a directory (recursive).
/// If a file alreay exists, it will be overwritten.
/// </summary>
/// <param name="strSrcdir">Directory to copy (eg: C:\myDirectory)</param>
/// <param name="strDestDir">Where to copy the directory
(D:\myNewDirectory). </param>
public static void CopyDirectory(string sourceDirectoryName, string
targetDirectoryName)
{
try
{
// Create directory if needed
Directory.CreateDirectory(targetDirectoryName);

// Treate all the file under current source directory
string[] files = Directory.GetFiles(sourceDirectoryName);
foreach(string fileName in files)
File.Copy(fileName, targetDirectoryName +
Path.DirectorySeparatorChar +
Path.GetFileName(fileName),true);

// now treat all subdirectory and their related files (called this
routine recursively)
string[] directories = Directory.GetDirectories(sourceDirectoryName);
foreach(string directory in directories)
CopyDirectory(directory, targetDirectoryName +
Path.DirectorySeparatorChar +
Path.GetFileName(directory));
}
catch (BTException ex0)
{
// This is an exception thrown while recursively be called. Just throw
further
throw ex0;
}
catch (Exception ex)
{
throw new BTException("Unable to Copy Directory " + sourceDirectoryName
+
" to " + targetDirectoryName + ". Check Path/File names. Err:
" + ex.Message,
Status.Stat.ECannotWriteFile, Severity.Level.Error);
}
}
"Antonio Lopez Arredondo" <ad**@someplace.com> wrote in message
news:eE**************@TK2MSFTNGP09.phx.gbl...
hi all !!!

I need to copy a folder and its subfolders to another location; which class should I use ?

could only find the System.IO.Directory.MOVE but don't know how to COPY.

thanks in advance,
ant

Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Casper B | last post by:
As the topic says, I am wondering how (in Java) I can extract a sub-tree of a org.w3c.dom.Document and form a brand new one. I use Oracle's XMLDocument implementation and have been playing around...
2
by: melo | last post by:
Hello, I've been struggling with a function(s) to recursively set all folders and files to NOT read-only. So, I thought I'd post this message. What I need to do is: given a starting path, I...
1
by: Antonio Lopez Arredondo | last post by:
hi all !!! I need to copy a folder and its subfolders to another location; which class should I use ? could only find the System.IO.Directory.MOVE but don't know how to COPY. thanks in...
8
by: Topper | last post by:
Hello. I have simple web folders structure: -ROOT - BIN WebService.dll WebService.asmx I need to use my WebService.dll not in bin folder - for example, in ROOT. How do i this? How can i do...
3
by: Guillaume Hanique | last post by:
Hi, I feel very stupid. I simply want to derive a control from system.web.ui.webcontrols.button and use that on my webform, but I just can't get it done. Can anyone tell me how to do that? I...
2
by: graphicsxp | last post by:
Hi, I have the following records stored in the database : TableName: Folder { FolderID, FolderName, TopLevel} TableName: FolderRelationship { ParentID, ChildID} This describes a folder...
9
by: pamela fluente | last post by:
What is the most current (for framework 2.0) and easy way to copy recursively all files from folder "Folder1" to folder "Folder2" ? Is there any simple function in the framework to do that? ...
1
by: dmitrey | last post by:
howto compile recursively all *.py files to *.pyc (from a directory my_dir)? Thank you in advance, D.
9
by: =?Utf-8?B?VGhvbWFzIFcuIEJyb3du?= | last post by:
"parez" wrote: Assembly.LoadWithPartialName does indeed work!! Now, why is this incredibly useful routine deprecated and we are instructed to use Assembly.Load instead which does not provide...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.