Code Man
Code Man

Reputation: 301

Moving files from one location to another

I'm trying to make this program that will move (cut and paste) all files from one directory (a folder) to another directory. In this example, I'm trying to move all the files that are inside the D:\Source folder (has a few files in it) to C:\Source folder (which has no files in it). When I run the program, I get this error.

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

Here is the full source code:

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

Upvotes: 0

Views: 4861

Answers (2)

Abhishek
Abhishek

Reputation: 7045

Exception clearly states that destinationPath is not valid. Make sure destinationPath exist as shown by @Mairaj then use File.Move to cut-paste. Complete code to move one file. You can your logic of directories to move all the files.

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

Find more info here

Upvotes: 0

Mairaj Ahmad
Mairaj Ahmad

Reputation: 14624

You need to check if destination directory exists than copy files otherwise first create destination directory.

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

Upvotes: 2

Related Questions