1,105,585 Community Members

Copy files from a folder to anothers by c#

Member Avatar
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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");
        }
    }
}
Member Avatar
sneekula
Nearly a Posting Maven
2,496 posts since Oct 2006
Reputation Points: 917 [?]
Q&As Helped to Solve: 263 [?]
Skill Endorsements: 5 [?]
 
0
 

I don't see the C!

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

Member Avatar
Dani
The Queen of DaniWeb
20,566 posts since Feb 2002
Reputation Points: 1,356 [?]
Q&As Helped to Solve: 931 [?]
Skill Endorsements: 204 [?]
Administrator
Featured
Sponsor
 
0
 

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".

Member Avatar
logiccool
Newbie Poster
2 posts since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Diamonddrake
Master Poster
721 posts since Mar 2008
Reputation Points: 396 [?]
Q&As Helped to Solve: 92 [?]
Skill Endorsements: 8 [?]
 
0
 

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.

Member Avatar
logiccool
Newbie Poster
2 posts since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
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

You
Post:
Start New Discussion
Tags Related to this Article