Rename a file in C#

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

How do I rename a file using C#?

18条回答
  •  不要未来只要你来
    2020-11-22 15:37

    I've encountered a case when I had to rename the file inside the event handler, which was triggering for any file change, including rename, and to skip forever renaming of the file I had to rename it, with:

    1. Making its copy
    2. Removing the original
    File.Copy(fileFullPath, destFileName); // both has the format of "D:\..\..\myFile.ext"
    Thread.Sleep(100); // wait OS to unfocus the file 
    File.Delete(fileFullPath);
    

    Just in case if someone, will have such scenario ;)

提交回复
热议问题