【问题标题】:Append timestamp and move a file C#附加时间戳并移动文件 C#
【发布时间】:2019-09-29 20:29:37
【问题描述】:

我正在尝试通过以下方式在名称上附加时间戳来移动和重命名文件:

private void MoveFile(string from, string to, string filename) {

    File.Move(from, System.IO.Path.Combine(to, filename + DateTime.Now.ToString().Replace(":", "-")));
}

我这样称呼它:

MoveFile(currentPath, outputFolderPath, System.IO.Path.GetFileName(currentPath));

这会导致以下异常:

抛出异常:“System.IO.DirectoryNotFoundException”在 mscoorlib.dll。

如果我删除时间戳的附加,它会起作用。为什么会出现此错误?

【问题讨论】:

  • DateTime.Now.ToString().Replace(":", "-") => 29/09/2019 21-32-07 - / 导致问题
  • @LeonardoSeccia 关注,谢谢!

标签: c# system.io.file


【解决方案1】:

如果你像这样使用你的函数:

MoveFile(@"C:\Users\Admin\Desktop\IMG_5628.png", @"C:\Users\Admin\Desktop", "IMG_5628.png");

那么您的路径将如下所示:

C:\Users\Admin\Desktop\IMG_5628.png30.09.2019 2-33-34

改变你的功能如下:

    MoveFile(@"C:\Users\Admin\Desktop\IMG_5628.png", @"C:\Users\Admin\Desktop", "IMG_5628.png");

    private void MoveFile(string from, string to, string filename)
    {
        File.Move(from, System.IO.Path.Combine(to, System.IO.Path.GetFileNameWithoutExtension(filename) + DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss") + System.IO.Path.GetExtension(filename)));
    }

而且你的文件的路径是有效的。

C:\Users\Admin\Desktop\IMG_56282019-30-9--02-39-03.png

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 2011-01-31
    相关资源
    最近更新 更多