简体   繁体   English

在 C# 中移动文件

[英]Move files in C#

I am moving some images (filenames are (1).PNG , (2).PNG and so on) from one directory to another.我正在将一些图像(文件名是(1).PNG(2).PNG等)从一个目录移动到另一个目录。 I am using the following code:我正在使用以下代码:

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.FileNotFoundException occurred in mscorlib.dll System.IO.FileNotFoundException类型的第一次机会异常发生在 mscorlib.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.png and so on.此外,我计划重命名文件,以便转换后的文件名将是00001.png00002.png 、... 00101.png等等。

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 PadLeft in 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
    }
}

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));
}

The exception means that the file E:\\vid(1).PNG doesn't exist.异常意味着文件E:\\vid(1).PNG不存在。 Do you mean E:\\vid1.PNG ?你的意思是E:\\vid1.PNG吗?

Use System.IO.Path class for constructing paths, it's better than concatenating strings.使用System.IO.Path类来构造路径,它比连接字符串更好。 You don't have to worry about escapting backslashes.您不必担心转义反斜杠。

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";

I just ran this in Visual Studio.我刚刚在 Visual Studio 中运行了这个。 It worked.有效。

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.也许当您将文件移动到 vid 目录以开始测试时,windows 去掉了括号。 (1).png became 1.png... I got a file not found error from that phenomenon... otherwise, your code is solid. (1).png 变成了 1.png ......我从那个现象中得到了一个文件未找到错误......否则,你的代码是可靠的。 My version is almost identical.我的版本几乎相同。

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.我相信这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM