如何将文件夹从一个目录移动到另一个目录而不删除C#中的源文件夹 [英] How to move a folder from one directory to another without deleting the source folder in C#
本文介绍了如何将文件夹从一个目录移动到另一个目录而不删除C#中的源文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在F:\ Source中有一些文件夹
我希望将其移至F:\Destination
移动文件后我想要同一目录中的Source文件夹
我尝试过:
I have some Folders in "F:\Source"
and i Want to move it to "F:\Destination"
After Moving the files i want the Source folder in the same Directory
What I have tried:
System.IO.Directory.Move(@"F:\Source\", @"F:\Destination");
这是我试过的但是在移动文件后我找不到F:\目录中的Source文件夹
请帮帮我
This is what i tried but after moving the files I cant find the Source folder in "F:\" Directory
Help me Please
推荐答案
我用google搜索复制目录c#并在大约10秒内发现这个
如何:复制目录| Microsoft Docs [ ^ ]
I googled "copy directory c#" and found this in about 10 seconds
How to: Copy Directories | Microsoft Docs[^]
Directory.Move [ ^ ]应该这样做。它将指定的文件夹及其子文件夹和文件移动到指定位置。
如果您只想复制而不移动,可以使用 - System.IO.File.Copy on循环中的每个文件或可能是 FileSystem.CopyDirectory [ ^ ]可以提供帮助。
或者如果您只想移动文件夹内容,请检查以下内容是否有帮助 -
directory - 使用c#将子文件夹中的所有文件移动到另一个文件夹 - Stack Overflow [ ^ ]
谢谢:)
Directory.Move[^] is supposed to do so. It moves the specified folder along with it's sub-folders and files to the specified location.
If you want just to copy and not move, you can use- System.IO.File.Copy on loop for each files or may be FileSystem.CopyDirectory[^] can help.
Or if you want just the folder contents to be moved, please check following if it helps-
directory - Move all files in subfolders to another folder using c# - Stack Overflow[^]
Thanks :)
这里有我尝试和测试的一些例程:
Here are some routines that I tried and tested:
/// <summary>
/// Recursively copy directory (XCOPY style), except file to skip.
/// Existing files will be overwritten.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="target">The target.</param>
/// <param name="skipFileName">File to skip.</param>
public static void CopyAll(string source, string target, string skipFileName)
{
var sourceDi = new DirectoryInfo(source);
var targetDi = new DirectoryInfo(target);
CopyAll(sourceDi, targetDi, skipFileName);
}
/// <summary>
/// Recursively copy directory (XCOPY style), except file to skip.
/// Existing files will be overwritten.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="target">The target.</param>
/// <param name="skipFileName">File to skip.</param>
public static void CopyAll(DirectoryInfo source, DirectoryInfo target, string skipFileName)
{
// Check if the target directory exists, if not, create it.
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}
// Copy each file into it’s new directory, except files to skip.
foreach (var fileInfo in source.GetFiles())
{
if (fileInfo.Name != skipFileName)
{
Debug.Print(@"Copying {0}\{1}", target.FullName, fileInfo.Name);
fileInfo.CopyTo(Path.Combine(target.ToString(), fileInfo.Name), true);
}
}
// Copy each subdirectory using recursion.
foreach (var subDir in source.GetDirectories())
{
if (subDir.Name != target.Name)
{
var nextTargetSubDir = target.CreateSubdirectory(subDir.Name);
CopyAll(subDir, nextTargetSubDir, skipFileName);
}
}
}
这篇关于如何将文件夹从一个目录移动到另一个目录而不删除C#中的源文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文