Copy Files - CSharp System.IO

CSharp examples for System.IO:File Copy

Description

Copy Files

Demo Code


using System.Threading;
using System.Windows.Forms;
using System.Diagnostics;
using System.Xml;
using System.IO.Compression;
using System.IO;/*from   w  w  w .  j  av  a2 s  .  com*/
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Configuration;
using System;

public class Main{
        public static void CopyFiles(string sourceDir, string targetDir, bool overWrite, bool copySubDir)
        {
            foreach (string sourceFileName in Directory.GetFiles(sourceDir))
            {
                string targetFileName = Path.Combine(targetDir, sourceFileName.Substring(sourceFileName.LastIndexOf(PATH_SPLIT_CHAR) + 1));

                if (File.Exists(targetFileName))
                {
                    if (overWrite == true)
                    {
                        File.SetAttributes(targetFileName, FileAttributes.Normal);
                        File.Copy(sourceFileName, targetFileName, overWrite);
                    }
                }
                else
                {
                    File.Copy(sourceFileName, targetFileName, overWrite);
                }
            }
        }
}

Related Tutorials