在 C# 中移动文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13621975/
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
Move files in C#
提问by MKS
I am moving some images (filenames are(1).PNG, (2).PNGand so on) from one directory to another. I am using the following code:
我把一些图片(文件名是(1).PNG,(2).PNG等)从一个目录到另一个。我正在使用以下代码:
for (int i = 1; i < n; i++)
{
try
{
from = "E:\vid\(" + i + ").PNG";
to = "E:\ConvertedFiles\" + i + ".png";
File.Move(from, to); // Try to move
Console.WriteLine("Moved"); // Success
}
catch (IOException ex)
{
Console.WriteLine(ex); // Write error
}
}
However, I am getting the following error:
但是,我收到以下错误:
A first chance exception of type System.IO.FileNotFoundExceptionoccurred in mscorlib.dll
System.IO.FileNotFoundExceptionmscorlib.dll 中发生类型的第一次机会异常
System.IO.FileNotFoundException: Could not find file 'E:\vid\(1).PNG'.
Also, I am planning to rename the files so that the converted file name will be 00001.png, 00002.png, ... 00101.pngand so on.
另外,我计划重命名文件,以便转换后的文件名是00001.png, 00002.png, ...00101.png等等。
采纳答案by Tobia Zambon
I suggest you to use '@'in order to escape slashes in a more readable way. Use also Path.Combine(...)in order to concatenate paths and PadLeftin order to have your filenames as your specifics.
我建议您使用'@'以便以更易读的方式转义斜杠。也可Path.Combine(...)用于连接路径并将PadLeft您的文件名作为您的具体信息。
for (int i = 1; i < n; i++)
{
try
{
from = System.IO.Path.Combine(@"E:\vid\","(" + i.ToString() + ").PNG");
to = System.IO.Path.Combine(@"E:\ConvertedFiles\",i.ToString().PadLeft(6,'0') + ".png");
File.Move(from, to); // Try to move
Console.WriteLine("Moved"); // Success
}
catch (IOException ex)
{
Console.WriteLine(ex); // Write error
}
}
回答by Jakub Konecki
The exception means that the file E:\vid(1).PNGdoesn't exist. Do you mean E:\vid1.PNG?
异常意味着该文件E:\vid(1).PNG不存在。你的意思是E:\vid1.PNG?
Use System.IO.Pathclass for constructing paths, it's better than concatenating strings. You don't have to worry about escapting backslashes.
使用System.IO.Path类来构造路径,它比连接字符串更好。您不必担心转义反斜杠。
回答by Gustav Klimt
i.ToString()
might help you. You are passing
可能会帮助你。你路过
from = "E:\vid\(" + i + ").PNG";
to = "E:\ConvertedFiles\" + i + ".png";
I as integer and concatenation doesn't work due to that
and instead of using \\, add @like this
由于这个原因,我作为整数和连接不起作用,
而不是使用\\,@像这样添加
from = @"E:\vid\(" + i + ").PNG";
回答by Keysharpener
Why don't you use something like this?
你为什么不使用这样的东西?
var folder = new DirectoryInfo(@"E:\vid\"));
if (folder.Exists)
{
var files = folder.GetFiles(".png");
files.toList().ForEach(f=>File.Move(from,to));
}
回答by BrianK
I just ran this in Visual Studio. It worked.
我刚刚在 Visual Studio 中运行了这个。有效。
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main()
{
int n = 3;
for (int i = 1; i < n; i++)
{
string from = "C:\vid\(" + i + ").PNG";
string to = "C:\ConvertedFiles\" + i + ".png";
{
try
{
File.Move(from, to); // Try to move
Console.WriteLine("Moved"); // Success
}
catch (System.IO.FileNotFoundException e)
{
Console.WriteLine(e); // Write error
}
}
}
}
}
}
Maybe when you were moving files into vid directory to begin the test, windows shaved off the parenthesis. (1).png became 1.png... I got a file not found error from that phenomenon... otherwise, your code is solid. My version is almost identical.
也许当您将文件移动到 vid 目录以开始测试时,windows 去掉了括号。(1).png 变成了 1.png ......我从那个现象中得到了一个文件未找到错误......否则,你的代码是可靠的。我的版本几乎相同。
回答by madhan seeman
var folder = new DirectoryInfo(sourcefolder);
if (folder.Exists)
{
var files = folder.GetFiles("*.png");
files.ToList().ForEach(f => File.Move(sourcefolder + f, newFolderName + f));
}
I believe this will help.
我相信这会有所帮助。

