将文件从一个文件夹移动到另一个 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19699473/
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
Moving files from one folder to another C#
提问by Anoushka Seechurn
Guys I am trying to move all files ending with _DONE into another folder.
伙计们,我正在尝试将所有以 _DONE 结尾的文件移动到另一个文件夹中。
I tried
我试过
//take all files of main folder to folder model_RCCMrecTransfered
string rootFolderPath = @"F:/model_RCCMREC/";
string destinationPath = @"F:/model_RCCMrecTransfered/";
string filesToDelete = @"*_DONE.wav"; // Only delete WAV files ending by "_DONE" in their filenames
string[] fileList = System.IO.Directory.GetFiles(rootFolderPath, filesToDelete);
foreach (string file in fileList)
{
string fileToMove = rootFolderPath + file;
string moveTo = destinationPath + file;
//moving file
File.Move(fileToMove, moveTo);
But on executing these codes i get an error saying.
但是在执行这些代码时,我收到一条错误消息。
The given path's format is not supported.
Where did I go wrong ?
我哪里做错了 ?
采纳答案by codemonkeh
Your slashes are in the wrong direction. On windows you should use back slashes. E.g.
你的斜线方向错误。在 Windows 上,您应该使用反斜杠。例如
string rootFolderPath = @"F:\model_RCCMREC\";
string destinationPath = @"F:\model_RCCMrecTransfered\";
回答by JaredReisinger
The array of file names returned from System.IO.Directory.GetFiles()
includes their full path. (See http://msdn.microsoft.com/en-us/library/07wt70x2.aspx) This means that appending the source and destination directories to the file
value isn't going to be what you expect. You'll end up with values like F:\model_RCCMREC\F:\model_RCCMREC\something_DONE.wav
in fileToMove
. If you set a breakpoint on the File.Move()
line, you could look at the values you are passing, which can help debug a situation like this.
从返回的文件名数组System.IO.Directory.GetFiles()
包括它们的完整路径。(请参阅http://msdn.microsoft.com/en-us/library/07wt70x2.aspx)这意味着将源目录和目标目录附加到file
值不会是您所期望的。你最终会得到类似F:\model_RCCMREC\F:\model_RCCMREC\something_DONE.wav
in 的值fileToMove
。如果File.Move()
在行上设置断点,则可以查看传递的值,这有助于调试此类情况。
Briefly, you'll need to determine the relative path from rootFolderPath
to each file in order to determine the proper destination path. Take a look at the System.IO.Path
class (http://msdn.microsoft.com/en-us/library/system.io.path.aspx) for methods that will help. (In particular, you should consider Path.Combine()
rather than +
for building paths.)
简而言之,您需要确定rootFolderPath
每个文件的相对路径,以确定正确的目标路径。查看System.IO.Path
类 ( http://msdn.microsoft.com/en-us/library/system.io.path.aspx) 以获取有帮助的方法。(特别是,您应该考虑Path.Combine()
而不是+
构建路径。)
回答by Vivekkumar Bangaru
Please try the below function. This works fine.
请尝试以下功能。这工作正常。
Function:
功能:
public static void DirectoryCopy(string strSource, string Copy_dest)
{
DirectoryInfo dirInfo = new DirectoryInfo(strSource);
DirectoryInfo[] directories = dirInfo.GetDirectories();
FileInfo[] files = dirInfo.GetFiles();
foreach (DirectoryInfo tempdir in directories)
{
Console.WriteLine(strSource + "/" +tempdir);
Directory.CreateDirectory(Copy_dest + "/" + tempdir.Name);// creating the Directory
var ext = System.IO.Path.GetExtension(tempdir.Name);
if (System.IO.Path.HasExtension(ext))
{
foreach (FileInfo tempfile in files)
{
tempfile.CopyTo(Path.Combine(strSource + "/" + tempfile.Name, Copy_dest + "/" + tempfile.Name));
}
}
DirectoryCopy(strSource + "/" + tempdir.Name, Copy_dest + "/" + tempdir.Name);
}
FileInfo[] files1 = dirInfo.GetFiles();
foreach (FileInfo tempfile in files1)
{
tempfile.CopyTo(Path.Combine(Copy_dest, tempfile.Name));
}
}
回答by Willy
I made it this way:
我是这样做的:
if (Directory.Exists(directoryPath))
{
foreach (var file in new DirectoryInfo(directoryPath).GetFiles())
{
file.MoveTo($@"{newDirectoryPath}\{file.Name}");
}
}
file is a type of FileInfo class. It already has a Method called MoveTo() that takes a destination path.
file 是一种 FileInfo 类。它已经有一个名为 MoveTo() 的方法,它采用目标路径。