Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto creating folders when using System.IO.File.Move

I'm updating an old winforms app which moves files to new locations using regex and System.IO.File.Move

Under windows 7, the old app worked fine. If a folder didn't exist, File.Move would create it

System.IO.File.Move("c:\stuff\a.txt","c:\stuff\a\file.txt");
System.IO.File.Move("c:\stuff\b.txt","c:\stuff\b\file.txt");
System.IO.File.Move("c:\stuff\c.txt","c:\stuff\c\file.txt");

However, under Windows 8 it seems that I have to manually create each folder in the path first. I get an error if I try and move to a folder that doesn't exist yet. Anyone know a way around this? I'd rather not have to create each folder

NOTE: The new, updated app is on WPF rather than winforms. Not sure if that's relevant

like image 959
roryok Avatar asked Aug 08 '14 10:08

roryok


People also ask

What happens when you move a file within a file system?

Illustrious. Whenever you move a file within the same drive/partition, it physically remains on the same spot. Only its path is changed within the file system on the drive.

Does file move overwrite?

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.

What is the correct method of moving a file from one location to another?

You can move a file or folder from one folder to another by dragging it from its current location and dropping it into the destination folder, just as you would with a file on your desktop. Folder Tree: Right-click the file or folder you want, and from the menu that displays click Move or Copy.


1 Answers

Before you File.Move() you could do:

new System.IO.FileInfo("c:\\stuff\\a\\file.txt").Directory.Create();

The above will create the "stuff" and "a" folders if they don't exist.

like image 107
Craig Avatar answered Sep 18 '22 13:09

Craig