场景

在C#中如果是删除文件的话可以直接使用

if (System.IO.File.Exists(fileName))
   {
         System.IO.File.Delete(fileName);
    }

 

但是如果要对指定路径下的文件进行重命名要怎么办。

注:

博客主页:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载

实现

if (System.IO.File.Exists(fileName))
   {
        FileInfo fi = new FileInfo(fileName);
        fi.MoveTo(newFileName); 
    }

 

注意:

fileName参数是原来的文件的全路径

newFileName是要修改的文件的全路径

比如这里知道了原来文件的全路径,可以使用

string filePath = Path.GetDirectoryName(strIdValue);

获取原有路径,然后再使用

string newFilePath = Path.Combine(filePath, newName);

将原有路径与新名字进行拼接

posted on 2019-11-19 17:24  霸道流氓  阅读(4163)  评论(0编辑  收藏  举报

Live2D