【问题标题】:Moving files from one location to another将文件从一个位置移动到另一个位置
【发布时间】:2015-04-29 07:28:52
【问题描述】:

我正在尝试制作这个程序,它将所有文件从一个目录(一个文件夹)移动(剪切和粘贴)到另一个目录。在此示例中,我试图将 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.");
            }    
        }
    }
}

【问题讨论】:

  • 发布错误中的实际文本,而不是随机图片链接,您还应该尝试仅包含与您遇到的实际问题相关的代码。
  • 仅供参考,您使用的是File.Copy,它不会剪切粘贴文件
  • 没有可用的移动方法,你必须复制文件然后自己删除它,因为移动过程实际上是复制然后删除,你必须自己做

标签: c# file directory location move


【解决方案1】:

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

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);
 }

【讨论】:

  • 这看起来像是我的答案。非常小的一点:严格来说Directory.CreateDirectory()做的第一件事就是检查它是否已经存在,所以你可以省略代码中的检查部分。
  • @MatthewWatson 感谢这个信息。我不知道这个。
  • @MatthewWatson 但我认为这里是必要的,因为目标目录可能只是一个目录,如果我们在不检查的情况下创建目录并且目录已经存在,则会引发异常。
  • 不,它不会因为Directory.CreateDirectory() 的实现首先检查目录是否已经存在,如果存在则返回 - 它不会抛出异常。
【解决方案2】:

异常明确指出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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 2011-03-16
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多