C#中的文件夹复制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1974019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Folder copy in C#
提问by venkat
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#.
我的机器的 C:\TEXTFILES\ 驱动器中有一个包含 10 个文本文件的文件夹。我想将文件夹 TEXTFILES 及其内容完全从我的机器复制到另一台机器。如何使用 C# 复制相同的内容。
采纳答案by Benny
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
来自MSDN
回答by Brij
- 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#
- 共享您的目标文件夹。
- 有很多方法可以做到这一点。请参阅以下内容:
使用 System.IO 在 C# 中复制文件夹 在 C# 中
复制目录的全部内容
回答by serhio
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
http://www.codeproject.com/KB/files/copydirectoriesrecursive.aspx
回答by Gaurav Shekhadiya
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));
}
}