We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,166 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Copy files from a folder to anothers by c#

0
By subcom on Jun 27th, 2008 8:27 am

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

I don't see the C!

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

sneekula
Nearly a Posting Maven
2,483 posts since Oct 2006
Reputation Points: 1,000
Solved Threads: 231
Skill Endorsements: 2

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

Dani
The Queen of DaniWeb
Administrator
21,343 posts since Feb 2002
Reputation Points: 1,555
Solved Threads: 367
Skill Endorsements: 122

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

logiccool
Newbie Poster
2 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

Diamonddrake
Master Poster
724 posts since Mar 2008
Reputation Points: 442
Solved Threads: 90
Skill Endorsements: 5
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

logiccool
Newbie Poster
2 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0723 seconds using 2.67MB