简体   繁体   中英

Copy a folder to another place using C#/WPF

I would like to explore directories , select a folder then copy a content of an existent folder to this new directory .

I am using this code , but it doesn't work out The folder that I wanna copy it is : C:\\Project

            DialogResult result = fd.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show(fd.SelectedPath.ToString());
            }

            var _SelectedPath = fd.SelectedPath.ToString();
            string sourceFile = @"C:\Project";
            string destinationFile = _SelectedPath;
            string fileName;

            //System.IO.Directory.Move(sourceFile, @"_SelectedPath");

            if (!System.IO.Directory.Exists(@"_SelectedPath"))
            {
                System.IO.Directory.CreateDirectory(@"_SelectedPath");
            }
            if (System.IO.Directory.Exists(sourceFile))
            {
                string[] files = System.IO.Directory.GetFiles(sourceFile);

                // Copy the files and overwrite destination files if they already exist. 
                foreach (string s in files)
                {
                    // Use static Path methods to extract only the file name from the path.
                    fileName = System.IO.Path.GetFileName(s);
                    _SelectedPath = System.IO.Path.Combine(@"_SelectedPath", fileName);
                    System.IO.File.Copy(s, @"_SelectedPath", true);
                }
            }

You could simplify thing a bit by adding a reference to Microsoft.VisualBasic as it handles Directory copy in a single function, I also has the ability to show the windows file copy progress dialog if needed.

Example:

DialogResult result = fd.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
    MessageBox.Show(fd.SelectedPath.ToString());
}

string _SelectedPath = fd.SelectedPath.ToString();
string destinationPath = @"C:\Project";

Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(_SelectedPath, destinationPath);

Replace your for loop with the following code

// Copy the files and overwrite destination files if they already exist. 
                foreach (string s in files)
                {
                    // Use static Path methods to extract only the file name from the path.
                    fileName = System.IO.Path.GetFileName(s);
                    string _currFileName = System.IO.Path.Combine(_SelectedPath, fileName);
                    System.IO.File.Copy(s, _currFileName, true);
                }

In your code you are appending filename to the same _SelectedPath every time. Consider if you have fileone.txt and filetwo.txt in your source directory (C:\\Test). When you enter the loop for the first time Filename will be C:\\Test\\fileone.txt . In the next iteration filename will be C:\\Test\\fileone.txt\\filetwo.txt which gives an error - file not found. Above code fixes the issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM