I have a folder structure:
C:\Temp [completely empty]
And I have a file that I want to move to
C:\Temp\Folder1\MyFile.txt
If I perform a File.Move I will get an error saying that this folder doesnt exist.
Is there any C# method that will create all the folders up to that point so:
C:\Temp\Folder1\
?
Move(String, String, Boolean) Moves a specified file to a new location, providing the options to specify a new file name and to overwrite the destination file if it already exists.
File. Move() doesn't support overwriting of an existing file. In fact, it will throw an IOException if a file with the same path as sourceDestFilename already exists.
Use System.IO.Directory.CreateDirectory
Addtional note: You don't have to check if it exists first. CreateDirectory will do the right thing regardless.
If Directory.Exists("somedir")
See here for more info.
To create a directory if it doesn't exist
Directory.CreateDirectory("path of dir");
It will create all dirs and subdirs, see here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With