Initially, my file is named "my file.txt"
var file = new FileInfo("C:\my file.txt");
The file is renamed to "My File.txt" while my code is running. How can my code refresh the file name?
file.Refresh();
does not update the spelling in Name property.
-- Edit --
Ok, I'll do this then
file = file.Directory.GetFiles(file.Name).First();
Case matters when an application lists the files as content to be inserted into a document and the customer asks for a certain case to be displayed.
The Refresh method only refreshes these attributes of the FileInfo instance. As you can see the filename isn't part of that.
What you can do is having a new implementation of a FileSystemInfo class that leverages a FileSystemWatcher to track renames of a file that is given to a FileInfo instance.
This is what the proof-of-concept looks like. In the constructor we also setup FileSystemWatcher for the directory the file is in. We filter for name changes and wireup the Renamed event of the FileSystemWatcher. If the name change is indeed for the file we track (done by doing an ignore case comparison of the filenames) we replace the current FileInfo instance with a new one.
This is how you would use it:
With this as result:
Ideally you would Dispose the FileSystemWatcher. I leave that as an exercise for the reader as that needs a Dispose implementation on the LiveFileInfo class.