473,396 Members | 2,154 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,396 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 3294
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.