追加时间戳并移动文件 C#

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

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。

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


慕尼黑的夜晚无繁华
浏览 42回答 1
1回答

慕田峪4524236

如果你像这样使用你的函数: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
打开App,查看更多内容
随时随地看视频慕课网APP