FileInfo Copy() method and progressbar

  • Thread starter Thread starter Stefan Turalski \(stic\)
  • Start date Start date
S

Stefan Turalski \(stic\)

Hi,

I used to use CopyTo() method from FileInfo class, does anyone know if there
is a way to bind this with progressbar, or with some sort of paintBox - to
show progress of moving/coping file ?
 
Hi,

Yes, thats give a some sort of solution,but from simple method I have to
programm some sort of multithread application ;-/
Thats all only for this progress bar - I was wondering if progressBar could
handle events by some sort of binding with this FileInfo method, what should
be provide when we use this together a lot..

btw. If I have to use CopyFileEx from kernel32 I prefer to use some sort of
networking and do this function kernel independent.
 
THe only other alternative I can think of is using Stream.Read/ Stream.Write
inside a static method and use a delegate for updating the amoutn copied.
I didn't compile it or test it but something like this is what i would do,
if you don't want to use pinvokes.
e.x.:

using System;
using System.IO;

namespace Foo
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class FileCopier
{
public delegate void AmountCopiedSoFarHandler(int value);

public static void Copy(string source, string destination,
AmountCopiedSoFarHandler callback)
{
const int bufferSize = 8192*8;
FileStream srcStream = null;
FileStream destStream = null;
try
{
srcStream = File.OpenRead(source);
destStream = File.Create(destination, bufferSize);
while(srcStream.Position < srcStream.Length)
{
byte[] data = new byte[bufferSize];
int amtRead = srcStream.Read(data, 0, bufferSize);
destStream.Write(data, 0, amtRead);
//return the amount written to so far
callback.DynamicInvoke(new object[] {destStream.Position});
}
destStream.Flush();
callback.DynamicInvoke(new object[] {destStream.Position});
}
finally
{
try
{
srcStream.Close();
}
catch{}
try
{
destStream.Flush();
}
catch{}
try
{
destStream.Close();
}
catch{}
}
}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
}
}
}
 
Back
Top