473,473 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3299
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
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,...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.