How do I rename a file using C#?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
I couldn't find approach which suits me, so i propose my version. Of course need input, error handling.
Use:
In my case, i want the name of the renamed file to be unique, so i add a datetime stamp to the name. This way, the filename of the 'old' log is always unique:
NOTE: In this example code we open a directory and search for PDF files with open and closed parenthesis in the name of the file. You can check and replace any character in the name you like or just specify a whole new name using replace functions.
There are other ways to work from this code to do more elaborate renames but my main intention was to show how to use File.Move to do a batch rename. This worked against 335 PDF files in 180 directories when I ran it on my laptop. This is spur of the moment code and there are more elaborate ways to do it.
Take a look at System.IO.File.Move, "move" the file to a new name.