File copy with progress

  • Thread starter Thread starter Johnny Jörgensen
  • Start date Start date
J

Johnny Jörgensen

Using File.Copy in the System.IO namespace, it's easy to copy one or more
files.

But I wonder: Is it possible to get windows to display its File Copy
Progress dialog at the same time - or alternatively get callbacks with the
progress?

Same goes for deleting files...

Cheers,
Johnny J.
 
Johnny Jörgensen said:
Using File.Copy in the System.IO namespace, it's easy to copy one or more
files.

But I wonder: Is it possible to get windows to display its File Copy
Progress dialog at the same time - or alternatively get callbacks with the
progress?

Same goes for deleting files...

This can be achieved by means of a call to the Shell function
ShFileOperation. You can do this from your .Net program by means of Platform
Invoke. The declaration, along with some sample code, can be found at
pinvoke.net:
http://www.pinvoke.net/default.aspx/shell32/SHFileOperation.html
 
Using File.Copy in the System.IO namespace, it's easy to copy one or more
files.

But I wonder: Is it possible to get windows to display its File Copy
Progress dialog at the same time - or alternatively get callbacks with the
progress?

Same goes for deleting files...

Cheers,
Johnny J.

Yes, using P/Invoke with SHFileOperation API is the long and risky way
to write the whole code error-free, that's why My namespace in VB.NET
provides a well-wrapper for this:

My.Computer.FileSystem.CopyFile("source","dest", _
True,FileIO.UICancelOption.DoNothing)

The third parameter, which is "showUI", lets you to view native dialog
while copying is in progress.

http://msdn.microsoft.com/en-us/library/36xbexyf(VS.80).aspx

Though yours is cross-post, you can call the same function in C# by
referencing it to Microsoft.VisualBasic.dll from .NET tab in "Add
Reference" menu with the following code:

using Microsoft.VisualBasic.Devices;

Computer copyfile = new Computer();
//Pass parameters below including showUI
copyfile.FileSystem.CopyFile(...);

Hope this helps,

Onur Güzel
 
My.Computer.FileSystem.CopyFile("source", "destination",
Microsoft.VisualBasic.FileIO.UIOption.AllDialogs,
FileIO.UICancelOption.DoNothing)
 
Yes, using P/Invoke with SHFileOperation API is the long and risky way
to write the whole code error-free, that's why My namespace in VB.NET
provides a well-wrapper for this:

My.Computer.FileSystem.CopyFile("source","dest", _
True,FileIO.UICancelOption.DoNothing)

Minor correction related to code above from myself, use that instead:

My.Computer.FileSystem.CopyFile _
("C:\source.txt", _
"C:\folder\dest.txt",
Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, _
FileIO.UICancelOption.DoNothing)

HTH,

Onur Güzel
 
Johnny Jörgensen said:
Using File.Copy in the System.IO namespace, it's easy to copy one or more
files.

But I wonder: Is it possible to get windows to display its File Copy
Progress dialog at the same time - or alternatively get callbacks with the
progress?

In addition to the other replies:

Wrapper around 'CopyFileEx' using the progress callback (suitable for
copying single files):

<URL:http://dotnet.mvps.org/dotnet/samples/filesystem/Transactions.zip>
 
Back
Top