I have some folders/files in several drive like
C:\New Folder
C:\a.txt
D:\New Folder\New Folder
D:\New Folder\a.txt
Now i want to create a directory tree in c drive that will look like
C:\mytempFolder\driveC\New Folder
C:\mytempFolder\driveC\a.txt
C:\mytempFolder\driveD\New Folder\New Folder
C:\mytempFolder\driveD\New Folder\a.txt
Is there any shortcut way to implement this in c#?
Use Directory.Create to create the directories:
and to create the files, you’ll have to write a text file to that location, like so:
(It returns a FileSream, so remember to close this afterwards or you will lock the file)
EDIT:
Ok I see what you mean. It that case you need to do something like this:
Loop all files in directory and its sub directories
and when you come across a file or folder, you’ll then need to create it in your target folder (i.e. c:\mytempFolder) using the above commands. To get the drive you can simply get the first character in the path string (assuming it will always have a drive letter).
Here’s a simple way of doing that but you may need to tweak it to account of filepath inconsistencies: