C#编程-115:文件夹操作之移动

100 阅读1分钟
  1. using System;

  2. using System.Collections.Generic;

  3. using System.IO;

  4. using System.Linq;

  5. using System.Text;

  6.  

  7. namespace MoveDerictoryTest

  8. {

  9.     class Program

  10.     {

  11.         static void Main(string[] args)

  12.         {

  13.             string pathSource = @"C:\Users\pengshiyu\Desktop\source\test";

  14.             string pathDestination = @"C:\Users\pengshiyu\Desktop\destination\test";

  15.  

  16.             //先判断源文件夹是否存在

  17.             if (Directory.Exists(pathSource))

  18.             {

  19.                 try

  20.                 {

  21.                     //如果目标文件夹存在则会抛出异常

  22.                     Directory.Move(pathSource, pathDestination);

  23.                     Console.WriteLine("文件夹移动成功");

  24.                 }

  25.                 catch (Exception ex)

  26.                 {

  27.  

  28.                     Console.WriteLine("文件夹移动失败:" + ex.Message);

  29.                 }

  30.             }

  31.             else

  32.             {

  33.                 Console.WriteLine("文件夹不存在");

  34.             }

  35.             Console.ReadKey();

  36.         }

  37.  

  38.     }

  39. }

C#编程-115:文件夹操作之移动
\