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:

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

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

move file create directory.的更多相关文章

  1. Git异常:fatal: could not create work tree dir 'XXX': No such file or directory

    GitHub实战系列汇总:http://www.cnblogs.com/dunitian/p/5038719.html ———————————————————————————————————————— ...

  2. linux,安装软件报错cannot create regular file '/usr/local/man/man1': No such file or directory

    make install时报错,如下 install: cannot create regular file '/usr/local/man/man1': No such file or direct ...

  3. adb pull 报错处理:adb: error: cannot create file/directory 'E:\': No such file or directory

    adb pull /sdcard/1.txt e:/ 报错:adb: error: cannot create file/directory 'E:\': No such file or direct ...

  4. C# IO流 File.Exists,Directory.Exists, File.Create,Directory.CreateDirectory

    void Start() { CreateDirectory(); CreateFile(); } //平台的路径(封装起来的一个属性,这不是一个方法) public string path { ge ...

  5. mkdir: Cannot create directory /file. Name node is in safe mode.

    刚刚在hadoop想创建一个目录的时候,发现报错了 具体信息如下: [hadoop@mini1 hadoop-2.6.4]$ hadoop fs -mkdir /file mkdir: Cannot ...

  6. opencv c++安装踩坑记录 file cannot create directory: /usr/local/include/opencv2. Maybe need administrative privileges

    前言 最近深度学习Ultra-Fast-Lane-Detection/INSTALL.md at master · cfzd/Ultra-Fast-Lane-Detection (github.com ...

  7. C#基础精华04(文件流,文件操作,File、Directory、Path,Directory)

    文件流 FileStream  可读可写  大文件  释放 StreamReader 读取   释放 StreamWriter 写入   释放 using 中释放 File 可读可写  小文件 操作文 ...

  8. python文件和文件夹訪问File and Directory Access

    http://blog.csdn.net/pipisorry/article/details/47907589 os.path - Common pathname manipulations 都是和路 ...

  9. File、Directory、Path

    File.Directory.Path https://blog.csdn.net/xiaouncle/article/details/52050577 File.Directory.Path是实际开 ...

随机推荐

  1. hibernate的速度问题--hibernate.jdbc.fetch_size和 hibernate.jdbc.batch_size

    hibernate的速度问题 这点我也疑惑过,最初应用hibernate的项目,我也感觉速度很慢,知道后来才知道问题的所在.       其实hibernate的速度性能并不差,比起jdbc来说,又是 ...

  2. css的定位机制

    牛腩新闻发不系统中遇到了CSS(Cascading style sheets),第一次接触,比较陌生还!因为CSS很多关于元素定位的问题,并且很多情况下元素的位置以像素精度计.一个不小心就很头疼,为此 ...

  3. Java中类Exchaner浅析

    Exchaner用于实现两个人之间的数据交换,每个人在完成一定的事物后想与对方交换数据,第一个先拿出数据的人将一直等待第二个人拿着数据到来时,才能彼此交换数据. 张孝祥老师在讲解Exchaner时的比 ...

  4. bzoj2466,poj1222

    都是简单的异或高斯消元 由于bzoj2466要求解得最小和,所以我们最后还要穷举自由元取最优解 type node=record        po,next:longint;      end; . ...

  5. HDOJ 1081(ZOJ 1074) To The Max(动态规划)

    Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle ...

  6. nginx做负载均衡器以及proxy缓存配置 - SegmentFault

    nginx做负载均衡器以及proxy缓存配置 - SegmentFault nginx做负载均衡器以及proxy缓存配置

  7. Validate XML using a XSD (XML Schema)

    Consider this XML file howto.xml : <?xml version="1.0" encoding="ISO-8859-1"? ...

  8. 【转】CPU调度

    转自:http://blog.csdn.net/xiazdong/article/details/6280345 CPU调度   用于多道程序 以下先讨论对于单CPU的调度问题. 回顾多道程序,同时把 ...

  9. php动态网页实现页面静态化 通过在初次被访问时生成html文件保存起来,下次该PHP程序被访问时就直接找到以前被访问过的html页面

    一.什么是静态页面?什么是动态页面 静态页面是网页的代码都在页面中,不需要执行asp,php,jsp,.net等程序生成客户端网页代码的网页.不能   静态页面 动态页面 区别:           ...

  10. Hadoop最基本的wordcount(统计词频)

    package com.uniclick.dapa.dstest; import java.io.IOException; import java.net.URI; import org.apache ...