Start New Discussion within our Software Development Community

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

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

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

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.

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