Click to See Complete Forum and Search --> : File copying
invader7
October 9th, 2009, 07:20 PM
Hello people, thanks for great advice and help so far. This time i'd like to know another way to copy files (different from File.Copy)
when i copy with File.Copy destination file has full file size (like source) from the 1 second of the copy.
i want to copy the file progressively so when i ask for it's file size to get different values as it is copied.
is there a way?
BigEd781
October 9th, 2009, 07:39 PM
So, if I am getting this, you would like to show some sort of progress indicator for large files, correct? I don't think that .NET provides a mechanism for this. You will need to perform the copy on another thread and use a delegate to report progress back to your UI. You can use the SHFileOperation API function through the P/Invoke layer, or just manually do the copy with a stream reader/writer.
invader7
October 9th, 2009, 08:45 PM
thanks for the answer, i searched for what you adviced me and i found how to do what i want...
String strFileName = source file name;
FileStream fsIn = File.OpenRead(strFileName);
FileStream fsOut = File.OpenWrite(destination file name);
Byte[] buf = new Byte[4096];
int intBytesRead;
while ((intBytesRead = fsIn.Read(buf, 0, 4096)) > 0)
fsOut.Write(buf, 0, intBytesRead);
fsOut.Flush();
fsOut.Close();
fsIn.Close();
BigEd781
October 9th, 2009, 09:34 PM
Just an FYI, you can wrap any object that implements IDisposable in a using block and omit the calls to dispose/close:
using ( FileStream fsIn = File.OpenRead( inFileName ) )
using ( FileStream fsOut = File.OpenWrite( destfilename ) )
{
// code here
}
// Dispose() is called on both objects when control leaves the above scope.
// this is the same as something like this:
try
{
FileStream fsIn = File.OpenRead(strFileName);
FileStream fsOut = File.OpenWrite(destination file name);
}
finally
{
fsIn.Dispose( );
fsOut.Dispose( );
}
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.