|
|
Comments and Discussions
|
|
 |
|
|
|

|
Hi,
Nice article, but may be better if u use Path.Combine to take care of string "\" to the folder paths when ever necessary.
|
|
|
|

|
Does not work correctly is the source folder has subfolder. All individual folders are dumped to the destination folder completely destroying the structure.
|
|
|
|
|
|

|
The (static) method in the tile (Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory) does the same thing and optionally provides the Windows Copy File Dialog with it.
Going to do something about this?
|
|
|
|

|
no comment.
|
|
|
|

|
<code>
private static void CopyDirectory(string trgDir, string srcDir)
{
string[] allFiles = Directory.GetFiles(srcDir, "*.*", SearchOption.AllDirectories);
foreach (string srcFile in allFiles) {
string targetFile = trgDir + srcFile.Substring(srcDir.Length);
string targetDir = Path.GetDirectoryName(targetFile);
if (Directory.Exists(targetDir)) {
Directory.CreateDirectory(targetDir);
}
File.Copy(srcFile, targetFile);
}
}
</code>
|
|
|
|

|
If you writing C# please use build-in tools like: Path.Combine()
you can change it here:
if(Dst[Dst.Length-1]!=Path.DirectorySeparatorChar)
Dst+=Path.DirectorySeparatorChar;
this is very disturbing to see code like this. Strings compare when you have build-in and (!) multyplatform command. So sad...
"I have not failed.
I've just found 10,000 ways that won't work."
- Thomas Alva Edison (1847-1931)
|
|
|
|

|
You would like to check if the destination is a sub directory of the source. Otherwise you'll get an infinite loop, which will fill up your disk completely in minutes. This is one solution: (sorry I renamed the variable names to company standards)
public static void CopyDirectory(string src, string dst)
{
String[] files;
if (dst[dst.Length - 1] != Path.DirectorySeparatorChar)
dst += Path.DirectorySeparatorChar;
if (!Directory.Exists(dst))
Directory.CreateDirectory(dst);
files = Directory.GetFileSystemEntries(src);
foreach (string element in files)
{
if (Directory.Exists(element))
{
if (element + Path.DirectorySeparatorChar != dst)
{
CopyDirectory(element, dst + Path.GetFileName(element));
}
}
else
{
File.Copy(element, dst + Path.GetFileName(element), true);
}
}
}
|
|
|
|

|
Thanks for the convinient func !
I applied try/catch + ret value indicates if the copy succeed.
here is the updated func :
///
/// Copy directory structure recursively
///
public bool copyDirectory(string Src, string Dst)
{
String[] Files;
bool bSucess = true;
if (Dst[Dst.Length - 1] != Path.DirectorySeparatorChar)
Dst += Path.DirectorySeparatorChar;
if (!Directory.Exists(Dst))
Directory.CreateDirectory(Dst);
Files = Directory.GetFileSystemEntries(Src);
foreach (string Element in Files)
{
try
{
// Sub directories
if (Directory.Exists(Element))
copyDirectory(Element, Dst + Path.GetFileName(Element));
// Files in directory
else
File.Copy(Element, Dst + Path.GetFileName(Element), true);
}
catch (Exception)
{
bSucess = false;
}
}
return bSucess;
}
|
|
|
|

|
Its such a common requirement im sure, but the .NET API doesnt support it. I googled for this functionality and hit this topic.
The code just works perfectly and I didnt even bother to understand how it was doing what it is doing. It seamlessly integrated into my program.
Thanks
Sreenath
|
|
|
|

|
I am working on a project that requires me to pick folders out of a source folder, and copy them to a destination. With both examples of the code for the copy, i'm getting the content of the folders i want copied into the root of the destiation without the folder being created.
for example
the path I am passing is src = \\test\share\nc11 and dest = \\test\dest\
what I'm getting is the contents of nc11 dumped into the root of dest
I want to copy nc11 to \\test\dest\
|
|
|
|

|
How can i exclude files .. i wonder because some hidden open file by someone else can't copy to mirror
|
|
|
|

|
This is precisely for what I was searching.
|
|
|
|

|
Hey's it's not that complex! Just use getfiles and getdirectories
// using System.IO;
private void CopyFolder( string source, string dest )
{
if (!dest.EndsWith("\\")) dest += "\\";
foreach (string file in Directory.GetFiles( source ))
{
File.Copy(file, dest + Path.GetFileName(file), true);
}
foreach (string folder in Directory.GetDirectories( source ))
{
string subFolder = Path.GetFileName(folder);
Directory.CreateDirectory(dest + "\\" + subFolder);
CopyFolder(folder, dest + "\\" + subFolder);
}
}
|
|
|
|

|
Quick and easy little method to do exactly what I needed. Thanks.
Ignore the cocky buttheads who constantly have to come in and criticize why you did it this way or how you should add this functionality to make it better.
There are only 10 types of people in this world....those that understand binary, and those that do not.
|
|
|
|

|
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;
namespace Project1
{
public class Util
{
public Util()
{
}
///
/// Filesystem
///
public class FileSystem
{
// Copy directory structure recursively
public static void copyDirectory(string Src,string Dst, string[] SrcFilterPattern)
{
ArrayList FileCol = null;
if(Dst[Dst.Length-1]!=Path.DirectorySeparatorChar)
Dst+=Path.DirectorySeparatorChar;
if(!Directory.Exists(Dst)) Directory.CreateDirectory(Dst);
FileCol = new ArrayList();
for (int i = 0; i < SrcFilterPattern.Length; i ++)
{
if (SrcFilterPattern[i].CompareTo("") == 0)
{
FileCol.AddRange(Directory.GetFileSystemEntries(Src));
}
FileCol.AddRange(Directory.GetFileSystemEntries(Src, SrcFilterPattern[i]));
}
foreach(string Element in FileCol)
{
// Sub directories
if(Directory.Exists(Element))
copyDirectory(Element,Dst+Path.GetFileName(Element), SrcFilterPattern);
// Files in directory
else
File.Copy(Element,Dst+Path.GetFileName(Element),true);
}
}
// Copy directory structure recursively
public static void copyDirectory(string sDir,string sDst, string fileFilterPattern)
{
try
{
Regex r = new Regex("(;|,)"); // Split on hyphens.
string[] fileFilters = r.Split(fileFilterPattern);
r = null;
copyDirectory(sDir, sDst, fileFilters);
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
// Copy directory structure recursively
public static void copyDirectory(string sDir,string sDst)
{
try
{
copyDirectory(sDir, sDst, "");
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
static void Main(string[] args)
{
try
{
//copyDirectory(@"c:\temp",@"c:\temp\new", "*e.exe;*l.xml");
copyDirectory(@"c:\temp",@"c:\temp\new");
// Directory.Delete(@"c:\MySrcDirectory") to mimic a Directory.Move behaviour
}
catch(Exception Ex)
{
Console.Error.WriteLine(Ex.Message);
}
}
}
}
}
|
|
|
|

|
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;
namespace Project1
{
public class Util
{
public Util()
{
}
///
/// Filesystem
///
public class FileSystem
{
// Copy directory structure recursively
public static void copyDirectory(string Src,string Dst, string[] SrcFilterPattern)
{
ArrayList FileCol = null;
if(Dst[Dst.Length-1]!=Path.DirectorySeparatorChar)
Dst+=Path.DirectorySeparatorChar;
if(!Directory.Exists(Dst)) Directory.CreateDirectory(Dst);
FileCol = new ArrayList();
for (int i = 0; i < SrcFilterPattern.Length; i ++)
{
if (SrcFilterPattern[i].CompareTo("") == 0)
{
FileCol.AddRange(Directory.GetFileSystemEntries(Src));
}
FileCol.AddRange(Directory.GetFileSystemEntries(Src, SrcFilterPattern[i]));
}
foreach(string Element in FileCol)
{
// Sub directories
if(Directory.Exists(Element))
copyDirectory(Element,Dst+Path.GetFileName(Element), SrcFilterPattern);
// Files in directory
else
File.Copy(Element,Dst+Path.GetFileName(Element),true);
}
}
// Copy directory structure recursively
public static void copyDirectory(string sDir,string sDst, string fileFilterPattern)
{
try
{
Regex r = new Regex("(;|,)"); // Split on hyphens.
string[] fileFilters = r.Split(fileFilterPattern);
r = null;
copyDirectory(sDir, sDst, fileFilters);
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
// Copy directory structure recursively
public static void copyDirectory(string sDir,string sDst)
{
try
{
copyDirectory(sDir, sDst, "");
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
static void Main(string[] args)
{
try
{
//copyDirectory(@"c:\temp",@"c:\temp\new", "*e.exe;*l.xml");
copyDirectory(@"c:\temp",@"c:\temp\new");
// Directory.Delete(@"c:\MySrcDirectory") to mimic a Directory.Move behaviour
}
catch(Exception Ex)
{
Console.Error.WriteLine(Ex.Message);
}
}
}
}
}
|
|
|
|

|
First of all its a great article and it saved me tons of time.
My task was to back up a huge folder on backup server. On production server I have a backup server's mapped drive as "s". Now everytime I try to backup the directory, I get an error message "Could not find a part of the path "s:\".
Any ideas how to take care of this issue?
Thanks
|
|
|
|

|
Hi !
I want to provide my client a utility that downloads a folder from my server and overwrite it on a pre-specified local folder without asking the user anything.
For this I am writing a windows application in C#. But I donot know how to download a whole folder(which may contain files and another folders) from server and overwrite it on a local folder.
Please help.
looking for a quick and positive response
-vikas
|
|
|
|

|
Public Sub CopyDir(ByVal Src As String, ByVal Dst As String)
Dim Files() As String, Element As String
If Microsoft.VisualBasic.Right(Dst, Dst.Length - 1) <> Path.DirectorySeparatorChar Then
Dst &= Path.DirectorySeparatorChar
End If
If Not Directory.Exists(Dst) Then Directory.CreateDirectory(Dst)
Files = Directory.GetFileSystemEntries(Src)
For Each Element In Files
If Directory.Exists(Element) Then
CopyDir(Element, Dst & Path.GetFileName(Element))
Else
File.Copy(Element, Dst & Path.GetFileName(Element), True)
End If
Next Element
End Sub
Starlogic
|
|
|
|

|
do u know how to determine drive types using C#. Just like what GetDriveTyes are doing in windows SDK.
|
|
|
|

|
First, thanks a lot for the article. I have a question about .NET framework. I really have no idea about the framework and if the question is so stupid you are free not to answer it
Well the question is can you use the Win API in a managed .NET program. If so, why don't you use the a simple shell function to copy the directory with all of its contents. Isn't it better? :?
Mustafa Demirhan
http://www.macroangel.com
Sonork ID 100.9935:zoltrix
They say I'm lazy but it takes all my time
|
|
|
|

|
First off, it's a great article that points out another thing that's wrong with the FCL. Now...if you don't mind, I'd like to critique it a bit.
First off, there's no reason to return bool. A better way to do it would be to use straight Exceptions. To put it in Rama's words: "It's not the way of the Framework." The System.IO.File.Copy() method is marked as public static void Copy(), so what you should do is make this return void and stick to using Exceptions.
Another thing, when you say:
catch(Exception Ex)
{
Console.Error.WriteLine(Ex.Message);
Success=false;
}
That's wrong. Your code, as part of a class library, should never visually display information...and that includes writing to the console. You should let the consumer handle that. Jeffrey Richter points this out in his book Applied Microsoft .NET Framework Programming, the DataGrid displays a MessageBox when there is an error in setting the CurrentCell property. I know that as an app-writer, I would hate to have a component or utility that I was using display visual information to the user without my consent.
Oh, and you don't need a constructor in your class unless there are other methods that are not marked as static. So I would remove that because really, this class should never be instantiated.
That being said, it's a great implementation for something that should be in the Framework.
You will now find yourself in a wonderous, magical place, filled with talking gnomes, mythical squirrels, and, almost as an afterthought, your bookmarks
-Shog9 teaching Mel Feik how to bookmark
I don't know whether it's just the light but I swear the database server gives me dirty looks everytime I wander past.
-Chris Maunder
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
|
Simple C#/.NET tip to copy an entire directory tree to another directory
| Type | Article |
| Licence | |
| First Posted | 18 Nov 2002 |
| Views | 274,655 |
| Bookmarked | 56 times |
|
|