move file create directory.

If we want to move file to the directory that does not exist,and if we perform a File.Move,it will result return an error shows that 'The path is not exist'.

So,we have to create a new directory using System.IO.Directory.CreateDirectory.

Here is an example:

 1 string path = txt_path.Text.Trim(); //Root of the path that contains files to be move.
 2             System.IO.DirectoryInfo dirRoot = new System.IO.DirectoryInfo(path);
 3             if (!dirRoot.Exists) {
 4                 MessageBox.Show("The directory is not exist!");
 5                 return;
 6             }
 7             System.IO.FileInfo[] files = dirRoot.GetFiles();
 8             foreach (var file in files) {   //Iterate create new directories.
 9                System.IO.DirectoryInfo dir = System.IO.Directory.CreateDirectory(file.DirectoryName + "\\" + System.IO.Path.GetFileNameWithoutExtension(file.Name));
10                 file.MoveTo(dir.FullName + "\\" + file.Name);
11             }

 Ref:http://stackoverflow.com/questions/6925561/whats-the-easiest-way-to-ensure-folder-exist-before-i-do-a-file-move

posted @ 2014-01-07 02:05  wonkju  阅读(218)  评论(0编辑  收藏  举报