Rename a file in C#

后端 未结 18 992
北荒
北荒 2020-11-22 15:05

How do I rename a file using C#?

18条回答
  •  耶瑟儿~
    2020-11-22 15:30

    I couldn't find approach which suits me, so i propose my version. Of course need input, error handling.

    public void Rename(string filePath, string newFileName)
    {
        var newFilePath = Path.Combine(Path.GetDirectoryName(filePath), newFileName + Path.GetExtension(filePath));
        System.IO.File.Move(filePath, newFilePath);
    }
    

提交回复
热议问题