如何从某个文件夹中复制子文件夹及其文件? [英] How to copy also subfolders and their files from an certain folder?
问题描述
有人可以帮助我如何使用此文本下方的代码复制用户桌面文件夹的子文件夹及其文件吗?
我暂时无法复制 \" + computer +" \c $ \Users\Desktop
文件夹,但不包含子文件夹及其文件。
string [] dirs = Directory.GetDirectories(" \\\\ " + computer +" 的 \\c $ \\Users\\ 强> 的 "); 强>
foreach(dirs中的字符串项目)
{
  FileInfo f = new FileInfo(item);
user2 =" \\\\ " +计算机+" \\ c $ \\Users\\ 强> 的 "
+ f.Name +" \\Desktop " ;;
  ;   if(Directory.Exists(user2))
  ;                  {
user =" C:\\Backup \\" +电脑+"\\" + f.Name +" \\Desktop " ;;
  ; Directory.CreateDirectory(用户);
DirectoryInfo文件夹=新的DirectoryInfo(user2);
FileInfo [] files = folder.GetFiles();
foreach(文件中的FileInfo文件)
{
string temppath = Path.Combine(user,file.Name);
         file.CopyTo(temppath,true);
  ;}
}
<
有一刻,我过去已经做过类似的事了......
啊你在这里:
public class FolderCopier
{
public static void CopyFolder(string path,string newpath)
{
var d = new DirectoryInfo(路径);
var files = d.GetFiles();
var directories = d.GetDirectories();
newpath = newpath.EndsWith(" \\")|| newpath.EndsWith(" /")?
" {newpath} {d.Name} \\" :
" {NEWPATH} \\ {d.Name} \\英寸;
Directory.CreateDirectory(newpath);
foreach(文件中的var文件)
{
if(!File.Exists(newpath + file.Name))
file.CopyTo(newpath + file.Name);
}
foreach(目录中的var目录)
{
CopyFolder(directory.FullName,newpath);
}
}
}如何看待方法为每个子目录调用自身。
问候,克里斯
Can someone help how I can also copy user's desktop folder's subfolders and their files with the code below this text?
For moment I can only copy files from \" + computer + "\c$\Users\Desktop folder but not the subfolders and their files.
string[] dirs = Directory.GetDirectories("\\\\" + computer + "\\c$\\Users\\");
foreach (string item in dirs)
{
FileInfo f = new FileInfo(item);
user2 = "\\\\" + computer + "\\c$\\Users\\"
+ f.Name + "\\Desktop";
if (Directory.Exists(user2))
{
user = "C:\\Backup\\" + computer + "\\" + f.Name + "\\Desktop";
Directory.CreateDirectory(user);
DirectoryInfo folder = new DirectoryInfo(user2);
FileInfo[] files = folder.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(user, file.Name);
file.CopyTo(temppath, true);
}
}
Hi,
one moment, I already did something like this in the past...
Ah here you are:
public class FolderCopier { public static void CopyFolder(string path, string newpath) { var d = new DirectoryInfo(path); var files = d.GetFiles(); var directories = d.GetDirectories(); newpath = newpath.EndsWith("\\") || newpath.EndsWith("/") ?
"{newpath}{d.Name}\\" :
"{newpath}\\{d.Name}\\"; Directory.CreateDirectory(newpath); foreach (var file in files) { if(!File.Exists(newpath + file.Name)) file.CopyTo(newpath + file.Name); } foreach (var directory in directories) { CopyFolder(directory.FullName, newpath); } } }How you see the method calls itself for every subdirectory.
Greetings, Chris
这篇关于如何从某个文件夹中复制子文件夹及其文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!