将文件从一个位置移动到另一个位置

时间:2015-04-29 07:28:53

标签: c# file directory location move

我正在尝试制作这个程序,将所有文件从一个目录(一个文件夹)移动(剪切和粘贴)到另一个目录。在这个例子中,我试图将D:\ Source文件夹中的所有文件(包含几个文件)移动到C:\ Source文件夹(其中没有文件)。当我运行程序时,我收到此错误。

http://s13.postimg.org/kuufg0gmu/error.jpg

以下是完整的源代码:

using System.IO;    
namespace FileManager
{
    public partial class Form1 : Form
    {
        string sourceDirectory = "";
        //string destinationDirectory = @"C:\Destination";
        string date = "";
        string[] filePaths;
        string destinationPath;

        public Form1()
        {
            InitializeComponent();
        }

        private void buttonClean_Click(object sender, EventArgs e)
        {
            // Get source directory
            sourceDirectory = textBoxDirectory.Text;
            // Get date of files
            date = textBoxDate.Text;
            // Get file paths
            if (Directory.Exists(sourceDirectory))
            {
                filePaths = Directory.GetFiles(@sourceDirectory, "*", SearchOption.AllDirectories);
                foreach (string sourcePath in filePaths)
                {
                    destinationPath = sourcePath.Remove(0, 1).Insert(0, "C");

                    File.Copy(sourcePath, destinationPath);

                    //MessageBox.Show(destinationPath);
                }
            }
            else
            {
                MessageBox.Show("Directory does not exist.");
            }    
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您需要检查目标目录是否存在而不是复制文件,否则首先创建目标目录。

foreach (string sourcePath in filePaths)
 {
  destinationPath = sourcePath.Remove(0, 1).Insert(0, "C");
   if(!Directory.Exists(destinationPath))
      Directory.CreateDirectory(destinationpath);
  File.Copy(sourcePath, destinationPath);
  //MessageBox.Show(destinationPath);
 }

答案 1 :(得分:0)

异常明确指出destinationPath无效。确保destinationPath存在,如@Mairaj所示,然后使用File.Move进行剪切粘贴。移动一个文件的完整代码。您可以使用目录逻辑移动所有文件。

using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";
        string path2 = @"c:\temp2\MyTest.txt";
        try 
        {
            if (!File.Exists(path)) 
            {
                // This statement ensures that the file is created, 
                // but the handle is not kept. 
                using (FileStream fs = File.Create(path)) {}
            }

            // Ensure that the target does not exist. 
            if (File.Exists(path2)) 
            File.Delete(path2);

            // Move the file.
            File.Move(path, path2);
            Console.WriteLine("{0} was moved to {1}.", path, path2);

            // See if the original exists now. 
            if (File.Exists(path)) 
            {
                Console.WriteLine("The original file still exists, which is unexpected.");
            } 
            else 
            {
                Console.WriteLine("The original file no longer exists, which is expected.");
            }           

        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}

查找更多信息here

相关问题