Copy files from a folder to anothers by c#

subcom 0 Tallied Votes 3K Views Share

Introduction
In this sample,It shows how to move files to another folder.

Background
It can be used in winForm applications.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace www.treaple.com
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            ImportGISFiles(folderBrowserDialog1.SelectedPath, txtDPath.Text, txtDFolder.Text);
        }

        public void ImportGISFiles(string sourcePath, string destinationPath, string destinationFolder)
        {
            if (Directory.Exists(destinationPath + destinationFolder))
                Directory.Delete(destinationPath + destinationFolder, true);

            Directory.CreateDirectory(destinationPath + "\\" + destinationFolder);

            string[] fileArray = Directory.GetFiles(sourcePath);
            string fileName = null;
            for (int i = 0; i < fileArray.Length; i++)
            {
                fileName = Path.GetFileName(fileArray);
                File.Copy(fileArray, destinationPath + "\\" + destinationFolder + "\\" + fileName, true);
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.
            Process.Start("http://www.treaple.com/About_Us.htm");
        }
    }
}
sneekula 969 Nearly a Posting Maven

I don't see the C!

BTW, not everybody knows this yet ~~~ gets(text); Very, very bad idea.

Dani 4,084 The Queen of DaniWeb Administrator Featured Poster Premium Member

When you enter "C#" as the syntax language it strips the pound sign and you end up with C. You have to write it as "csharp".

logiccool 0 Newbie Poster

please add some comments to this code to make it understandable, please explain line 37 of your code
Thanks

Diamonddrake 397 Master Poster

There seems to be an error, you use filearray like a string when it should be used as an array. and your for loop does the same thing over and over again, it should be

string[] fileArray = Directory.GetFiles(sourcePath);
            string fileName = null;
            for (int i = 0; i < fileArray.Length; i++)
            {
                fileName = Path.GetFileName(fileArray[i]);
                File.Copy(fileArray[i], destinationPath + "\\" + destinationFolder + "\\" + fileName, true);
            }

even then you are creating and assigning an unnecessary variable. should simply be.

string[] fileArray = Directory.GetFiles(sourcePath);
            for (int i = 0; i < fileArray.Length; i++)
            {
                File.Copy(fileArray[i], destinationPath + "\\" + destinationFolder + "\\" + Path.GetFileName(fileArray[i]), true);
            }

Furthermore, another way would be to use the FileInfo class for copying files. Regardless, Thanks for sharing.

logiccool 0 Newbie Poster
string filename;
            string dpath = @"D:\TNP Project\TnP\Resources\";
            if (openFileDialog1.FileName != "")
            {
                
                filename = openFileDialog1.SafeFileName;
                File.Copy(openFileDialog1.FileName.ToString(), dpath + filename);
            }

This is sufficient for copying file from one location to another

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.