mercredi 29 avril 2015

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://ift.tt/1zciZMD

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

Aucun commentaire:

Enregistrer un commentaire