I want to copy a file to a new file name. Sometimes the source file might be a symbolic (file) link, created with
mklink C:\MyPath\ThisIsASymbolicLink.xml C:\MyPath\ThisIsTheOriginal.xml
I’m using this code:
string from = @"C:\MyPath\ThisIsASymbolicLink.xml";
string to = @"C:\MyPath\WantCopyOfOriginalFileHere.xml";
File.Copy(from, to, true);
However, I receive an IOException
The name of the file cannot be resolved by the system.
when the from file is really a symbolic link.
How can I code for the cases where the source file might be a real file, or a symbolic link to a file?
Expanding on this blog post, I created extension methods that take a DirectoryInfo or FileInfo that can refer to either an original or a symbolic link, and return a string indicating the fully qualified path name of the original file.
App Code
The application code is modified as follows:
Extension Method Code
Unit Tests
Batch File to Create Unit Test Data
Must be run as Administrator… a requirement of mklink.