I have a folder with 10 text files at C:\TEXTFILES\ drive in my machine. I want to copy the folder TEXTFILES and its contents completely from my machine to another machine. How to copy the same using C#.
Asked
Active
Viewed 6.7k times
13
-
Please help me with some sample code. As i need to copy the folder itself from one server machine to another server machine – venkat Dec 29 '09 at 11:03
-
@sukmar, there is no way to copy a directory using a single .NET function, this is because of security. You will need to write your own function. See my answer for details. – serhio Dec 29 '09 at 11:33
-
.NET could give us this "high level" method at the framework. – alansiqueira27 Sep 23 '13 at 15:14
4 Answers
35
using System;
using System.IO;
class DirectoryCopyExample
{
static void Main()
{
DirectoryCopy(".", @".\temp", true);
}
private static void DirectoryCopy(
string sourceDirName, string destDirName, bool copySubDirs)
{
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
DirectoryInfo[] dirs = dir.GetDirectories();
// If the source directory does not exist, throw an exception.
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
// If the destination directory does not exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// Get the file contents of the directory to copy.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
// Create the path to the new copy of the file.
string temppath = Path.Combine(destDirName, file.Name);
// Copy the file.
file.CopyTo(temppath, false);
}
// If copySubDirs is true, copy the subdirectories.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
// Create the subdirectory.
string temppath = Path.Combine(destDirName, subdir.Name);
// Copy the subdirectories.
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
}
From MSDN

Pete Kirkham
- 48,893
- 5
- 92
- 171

Benny
- 8,547
- 9
- 60
- 93
-
This looks fine if both source and destination of the machines are connected. But the above code throws exception if the Destination machine is not connected or it may expect user credentials to connect. Then in this type of scenarios the above code gets fails. Is there a way to overcome this type of exceptions occured during file transfer from machine to machine. – venkat Dec 30 '09 at 05:15
-
4What do you expect the outcome to be if the destination machine is not connected or expects credentials to complete the operation? I would think that an exception would be the appropriate behavior if the destination machine isn't available as I can't think of any way to force the destination to be online. If credentials are required then perhaps a dialog to request the credentials could be shown. – Mike Chess Dec 30 '09 at 05:52
-
Solution doesn't work if there are 2 levels of Folders hierarchy. It just copies it to the base folder. – whihathac Sep 03 '15 at 22:43
9
private void copyDirectory(string strSource, string strDestination)
{
if (!Directory.Exists(strDestination))
{
Directory.CreateDirectory(strDestination);
}
DirectoryInfo dirInfo = new DirectoryInfo(strSource);
FileInfo[] files = dirInfo.GetFiles();
foreach(FileInfo tempfile in files )
{
tempfile.CopyTo(Path.Combine(strDestination,tempfile.Name));
}
DirectoryInfo[] directories = dirInfo.GetDirectories();
foreach(DirectoryInfo tempdir in directories)
{
copyDirectory(Path.Combine(strSource, tempdir.Name), Path.Combine(strDestination, tempdir.Name));
}
}

Scott
- 21,211
- 8
- 65
- 72

Gaurav Shekhadiya
- 106
- 1
- 3
2
string path = @"C:\TEXTFILES\";
string path2 = @"P:\myNetworkPath\tesssst";
try
{
Directory.CreateDirectory(path2);
foreach (string fileName in Directory.GetFiles(path))
{
File.Copy(
Path.Combine(path, fileName),
Path.Combine(path2, fileName), true);
}
}
catch
{
Console.WriteLine("Exception");
}
For a deeper copy, see:
http://www.codeproject.com/KB/files/copydirectoriesrecursive.aspx

serhio
- 28,010
- 62
- 221
- 374
1
- Share your destination folder.
- There are a lot of ways to do this. See followings:
Copy Folders in C# using System.IO
Copy the entire contents of a directory in C#