Click here to Skip to main content
6,305,776 members and growing! (18,257 online)
Email Password   helpLost your password?
Desktop Development » Files and Folders » File System     Intermediate

Quick and dirty directory copy

By Tareq_Gamal

Quick directory copy to another directory.
C#, Windows, .NET 1.1VS.NET2003, Dev
Posted:17 Jan 2006
Views:36,705
Bookmarked:24 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
8 votes for this article.
Popularity: 2.44 Rating: 2.70 out of 5
2 votes, 25.0%
1
1 vote, 12.5%
2
1 vote, 12.5%
3
1 vote, 12.5%
4
3 votes, 37.5%
5

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 :)

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.

public string File_Copy(string strSource, 
          string strDestination,FileMode mode, 
          int bufferSize,int Action)
{
    byte[] Readbytes;
    Readbytes = new byte;
    ///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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Tareq_Gamal


Member

Location: Egypt Egypt

Other popular Files and Folders articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
GeneralPreserving the file attributes Pinmember248912820:49 28 Apr '08  
GeneralRe: Preserving the file attributes PinmemberTareq_Gamal0:26 6 May '08  
GeneralThere has many bugs Pinmember057_10011:39 18 Jul '07  
AnswerRe: There has many bugs PinmembertareqGamal3:31 24 Jan '08  
Generalcopy to 0 PinmemberDavidDou7:13 8 Feb '07  
GeneralRe: copy to 0 PinmembertareqGamal11:10 9 Mar '07  
GeneralI Love it PinmemberENG-HAMAD5:38 8 Nov '06  
AnswerRe: I Love it [modified] PinmemberTareq_Gamal10:51 8 Nov '06  
GeneralVS2005 PinmembertareqGamal1:58 1 Feb '06  
GeneralRe: VS2005 [modified] PinmemberTareq_Gamal8:20 13 Feb '06  
GeneralDoes not work!! PinmemberAndrew Y18:40 23 Jan '06  
GeneralRe: Does not work!! PinmemberTareq_Gamal0:31 24 Jan '06  
GeneralQ: DLL dependencies? PinmemberBilou_Gateux23:41 22 Jan '06  
GeneralRe: Q: DLL dependencies? PinmemberTareq_Gamal0:20 23 Jan '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Jan 2006
Editor: Smitha Vijayan
Copyright 2006 by Tareq_Gamal
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project