为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕尼黑的夜晚无繁华 2024-01-20 22:23:46
我正在尝试通过以下方式将时间戳附加到名称来移动和重命名文件: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。如果我删除时间戳的附加,它就会起作用。为什么会出现此错误?
查看完整描述

1 回答

?
慕田峪4524236

TA贡献1875条经验 获得超5个赞

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


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


查看完整回答
反对 回复 2024-01-20
  • 1 回答
  • 0 关注
  • 37 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信