![]() |
Desktop Development »
Files and Folders »
File System
Intermediate
Quick and dirty directory copyBy Tareq_GamalQuick directory copy to another directory. |
C#, Windows, .NET 1.1VS.NET2003, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
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.
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);
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
}
Current features:
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.
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.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
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 |