I've got an absolute path available to me. Say: C:/Program/CoreFiles/Folder1/Folder2/File.txt
.
I need to copy that file to C:/Program/Projects/ProjectName/
but it needs to keep Folder1/Folder2/File.txt
intact. So the end result should be C:/Program/Projects/ProjectName/Folder1/Folder2/File.txt
.
My first attempt at solving this was to try and get the relative path between 2 absolute paths. Found Path.GetRelativePath(string, string) which obviously didn't help as it wasn't meant for WinForms. It would mess up anyway as the final result would be C:/Program/Projects/ProjectName/CoreFiles/Folder1/Folder2/File.txt
.
The target directory is empty and I don't know the relative path to copy beforehand other than somehow getting that info out of the absolute path. Since File.Copy
won't create folders that don't exist yet, I need to create them first. So how do I get the path that leads up to the file from the CoreFiles
directory out of the absolute path?
The only working solution I can come up with is using regex to just replace CoreFiles
with Projects/ProjectName
in the path string and work with that. But that somehow seems the wrong approach.