附加时间戳并移动文件C#
我试图通过以下方式在文件名后附加时间戳来移动和重命名文件:附加时间戳并移动文件C#,c#,system.io.file,C#,System.io.file,我试图通过以下方式在文件名后附加时间戳来移动和重命名文件: 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,
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));
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
这将导致以下异常:
引发异常:中的“System.IO.DirectoryNotFoundException”
mscoorlib.dll
如果我删除时间戳的附加,它就会工作。为什么会出现此错误?如果您这样使用函数:
MoveFile(currentPath, outputFolderPath, System.IO.Path.GetFileName(currentPath));
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(currentPath, outputFolderPath, System.IO.Path.GetFileName(currentPath));
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
DateTime.Now.ToString().Replace(“:”,“-”)
=>29/09/2019 21-32-07
-导致issue@LeonardoSeccia很好,谢谢!