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

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. JENKINS的远程API调用,然后用PYTHON解析出最新的版本及稳定成功的版本

    这个功能,我觉得在作自动作部署时,是可以派上用处的. 记录一下. import urllib f = urllib.urlopen('http://jenkinsurl/job/job_name/ap ...

  2. VS2010编译、调用Lua程序

    一) .建立lua源代码工程,编译lua的静态库 1.下载Lua源码 http://www.lua.org/download.html a 下载后解压到一个目录下,这里假设解压到D:\lua-5.1. ...

  3. Android SwitchCompat 自定义颜色及使用

    在Android 5.0 中 Switch 更新了样式 变得 比较好用了 但是在5.0 以下的版本 还是老样子 不实用 因此 就有了 SwitchCompat 来兼容 它是v7 包中的 因此可兼容到 ...

  4. leetcode面试准备:Multiply Strings

    1 题目 Given two numbers represented as strings, return multiplication of the numbers as a string. Not ...

  5. 通过Delphi获得qq安装路径

    procedure TForm1.Button2Click(Sender: TObject); var Reg:TRegistry; Val:TStrings; ii:System.Integer; ...

  6. git日志的查看与修改

    1.命令行中查看日志 git log 默认是显示所有的日志信息,之前出来的界面显示的日志,很少. 最后发现,只需要使用键盘上向下键↓,就可以继续浏览更多的日志 空格键,可以翻页浏览日志. 向左←  向 ...

  7. configure: error: cannot find protoc, the Protocol Buffers compiler

    centos 6 安装mosh 1.2 2012-05-07 17:21:41标签:centos mosh 关于mosh(引用于) 芬兰研究员Tatu Ylönen于1995年设计出最早的SSH协议, ...

  8. Maven学习(2) - Maven构建多模块Java工程

    概述 项目开发时,通常会将项目分为多个模块进行开发,本文讨论如何用Maven构建多模块的Java工程. 软件环境 Java:1.6.0_26 Maven:3.1.1 OS:WindowXP SP3 项 ...

  9. SSH隧道技术----端口转发,socket代理

    原文的原始出处不详,本文也是在复制引用了某篇转载,并做了必要的整理与编辑. 本文的受众 如果你遇到了以下问题,那么你应该阅读这篇文章 我听说过这种技术,我对它很感兴趣 我想在家里访问我在公司的机器(写 ...

  10. JVM性能调优-GC内存模型及垃圾收集算法

    JVM内存管理模型: http://developer.51cto.com/art/201002/184385.htm 一 JVM内存模型 1.1 Java栈 Java栈是与每一个线程关联的,JVM在 ...