Click here to Skip to main content
15,881,281 members
Articles / Programming Languages / C#
Article

Quick and dirty directory copy

Rate me:
Please Sign up or sign in to vote.
2.81/5 (9 votes)
17 Jan 2006CPOL2 min read 71.6K   1.6K   28   17
Quick directory copy to another directory.

Image 1

Introduction

I saw a program to speed up the file copying process but I wanted to add some features to it, like, skip overwriting existing files, or no copying of existing files. Also, I wanted to control and monitor the copy process by a better method.

Background

I think you have to know how to manage a thread, and must be familiar with memory buffers. That's all :)

C#
File.Read(bytesArray,Offset ,bufferSize);

Using the code

This code abstractly copies files from one path to another path, by read portions (equal to the buffer size) then writing it on the destination file, then looping the same for all files on the source directory. It displays progress information for a file, for the current directory, and for the entire copy process.

C#
public string File_Copy(string strSource, 
          string strDestination,FileMode mode, 
          int bufferSize,int Action)
{
    byte[] Readbytes;
    Readbytes = new byte[bufferSize];
    ///1 read file Binary 
    ///
    FileStream Sfs = File.OpenRead(strSource);
    Dfs = File.Open(strDestination,mode);
    FileInfo Sinfo = new FileInfo(Sfs.Name);
    FileInfo Dinfo=new FileInfo(Dfs.Name);
    //
    //Set Progress File Name On Form1
    Form1.progCurrentFile.Text = strSource;
    while (Sfs.Length - Sfs.Position>=bufferSize)
    {
        //Read Buffer
        Sfs.Read(Readbytes,0,bufferSize);
        //Write Buffer
        Dfs.Write(Readbytes,0,bufferSize);
        UpdatePersentageOfFile(Sfs.Position,Sfs.Length);

        ///try to change Buffer Size at Copping file 
        ///
        bufferSize = Settings.bufferSize;
        Readbytes = null;
        Readbytes = new  byte[Settings.bufferSize];
        //end try 
        System.Windows.Forms.Application.DoEvents();

        // Write the Reminder to buffer If any
        if (Sfs.Position < Sfs.Length)
        {
            Sfs.Read(Readbytes,0, 
              int.Parse(Sfs.Length.ToString()) - 
              int.Parse(Sfs.Position.ToString()));
            Dfs.Write(Readbytes,0, 
              int.Parse(Sfs.Length.ToString()) - 
              int.Parse(Sfs.Position.ToString()));
            UpdatePersentageOfFile(Sfs.Position,Sfs.Position);
            TotalCopiedSize+=Sfs.Length;
            //Check for program Changes
            //FW
            //Do Events 
            System.Windows.Forms.Application.DoEvents();
        }
    }
    //end write 
}

Points of Interest

Current features:

  1. Copy files/directories from source directory to distention directory.
  2. Control memory buffer to speed up the copy process.
  3. Scan source directory to see all the files and directories inside, and get abstract info about the source directory.
  4. Pause / resume / cancel the copy process.
  5. Progress bars for a file, directory, and the copy process.
  6. In case of a damaged file found, it doesn't skip the copy process as Windows does. But it tells the user about it and the user can choose from one of the following options: write whatever can be written from the source file to the destination file /skip this file /skip all damaged files /write whatever can be written for all damaged files found.

Using the executable

  1. Copy
    • Click on the Source button to select the source directory.
    • Click on the Destination button to select where you want to copy it to.

    Now you can click Copy to start copying files. You may click on Progress to see how fast the files are copied and the actual percentage of completion. You may change the buffer size to speed up / slow down the copy process. You can also pause /resume /cancel the copy process at any time.

  2. Get information of a directory

    You can use this application to get information about a particular directory, like, how many files are inside a directory, the names of files, size information, etc.

    • Select a source directory.
    • Click on the Info button.
    • You may also select the Info tab to see an information display about the source directory.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader ArabicRobotics.com
Egypt Egypt
Tareq Gamal El-din Mohammed,
---------
Website:
www.ArabicRobotics.com

---------

Graduated from Modern Academy for Computer science and Information Technology. Egypt,
Then flow Microsoft development track Certificates:
MCAD.NET (Microsoft Certified Application Developer)
MCSD.NET (Microsoft Certified Solution Developer)
Microsoft SharePoint Administration, Configuration and Development.

Robotics fields was a Hobby since 2002,
started to develop some applications for "Robosapien", "RoboSapienV2", "RS Media", RoboMe and WowWee Rovio. from WowWee company,

Started working with robots as a professional way at 2014
By using "NAOqi" Robotics from Aldebaran.

By developing some applications and libraries like :
NAO.NET.
https://www.youtube.com/watch?v=oOyy-2XyT-c

OpenCV with NAO Robot:

- NAORobot Vision using OpenCV -TotaRobot P1
https://www.youtube.com/watch?v=MUcj8463x08

- NAO Robot Vision using OpenCV - P2
https://www.youtube.com/watch?v=93k1usaS-QM

NAO Alarm Clock :
https://www.youtube.com/watch?v=djLlMeGLqOU
-----------------------------

also Robotic Arm Project:


Other Projects Developed by Tareq Gamal El-din Mohammed :

Developed and posted some applications in Code Project web site like :

- Control your Distributed Application using Windows and Web Service
http://www.codeproject.com/Articles/101895/Control-your-Distributed-Application-using-Windows


- Quick and dirty directory copy
http://www.codeproject.com/Articles/12745/Quick-and-dirty-directory-copy

- Program Execute Timer(From the Web)
http://www.codeproject.com/Articles/12743/Program-Executer-Timer

Comments and Discussions

 
Questionwhat about 2010 and c# 4 Pin
Tareq_Gamal11-Oct-10 21:40
Tareq_Gamal11-Oct-10 21:40 
I think to Develop and deliver another enhanced version using VS 2010
and c# 4
it will have
* recover all buges in the current version
* many more features
* Inhanced & clever Interface
* Very dynamic
Who would like it? and like this idea ?Thumbs Up | :thumbsup:
Wellcome

SuggestionRe: what about 2010 and c# 4 Pin
Nhilesh B10-Oct-11 2:21
Nhilesh B10-Oct-11 2:21 
GeneralRe: what about 2010 and c# 4 Pin
tareqGamal19-Nov-11 10:08
tareqGamal19-Nov-11 10:08 
GeneralPreserving the file attributes Pin
248912828-Apr-08 19:49
248912828-Apr-08 19:49 
GeneralRe: Preserving the file attributes Pin
Tareq_Gamal5-May-08 23:26
Tareq_Gamal5-May-08 23:26 
GeneralThere has many bugs Pin
057_100118-Jul-07 0:39
057_100118-Jul-07 0:39 
AnswerRe: There has many bugs Pin
tareqGamal24-Jan-08 2:31
tareqGamal24-Jan-08 2:31 
Generalcopy to 0 Pin
DavidDou8-Feb-07 6:13
DavidDou8-Feb-07 6:13 
GeneralRe: copy to 0 Pin
tareqGamal9-Mar-07 10:10
tareqGamal9-Mar-07 10:10 
GeneralI Love it Pin
ENG-HAMAD8-Nov-06 4:38
ENG-HAMAD8-Nov-06 4:38 
AnswerRe: I Love it [modified] Pin
Tareq_Gamal8-Nov-06 9:51
Tareq_Gamal8-Nov-06 9:51 
GeneralVS2005 Pin
tareqGamal1-Feb-06 0:58
tareqGamal1-Feb-06 0:58 
GeneralRe: VS2005 [modified] Pin
Tareq_Gamal13-Feb-06 7:20
Tareq_Gamal13-Feb-06 7:20 
GeneralDoes not work!! Pin
Andrew Y23-Jan-06 17:40
Andrew Y23-Jan-06 17:40 
GeneralRe: Does not work!! Pin
Tareq_Gamal23-Jan-06 23:31
Tareq_Gamal23-Jan-06 23:31 
QuestionQ: DLL dependencies? Pin
Bilou_Gateux22-Jan-06 22:41
Bilou_Gateux22-Jan-06 22:41 
AnswerRe: Q: DLL dependencies? Pin
Tareq_Gamal22-Jan-06 23:20
Tareq_Gamal22-Jan-06 23:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.