473,382 Members | 1,431 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,382 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 3293
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...
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.