PDA

Click to See Complete Forum and Search --> : C#[2003] : Show Copy Progress Dialog While copying Files.


Shuja Ali
Feb 20th, 2006, 06:08 AM
We have always wanted to have those progress bars that Windows Comes up with while copying/moving/deleting Files. In C# we usually tend to use File.Copy function but this does not show us the progress of the File Copying. IN order to add this type of functionality to C# application we can use SHFileOperation API. Here is a class that uses the SHFileOPeration API to copy files from one location to another

using System;
using System.Runtime.InteropServices;
public class FileCopy
{
#region "API Declaration"
private enum FO_Func : uint
{
FO_MOVE = 0x0001,
FO_COPY = 0x0002,
FO_DELETE = 0x0003,
FO_RENAME = 0x0004,
FOF_ALLOWUNDO = 0x0040
}

private struct SHFILEOPSTRUCT
{
public IntPtr hwnd;
public FO_Func wFunc;
[MarshalAs(UnmanagedType.LPWStr)]
public string pFrom;
[MarshalAs(UnmanagedType.LPWStr)]
public string pTo;
public ushort fFlags;
public bool fAnyOperationsAborted;
public IntPtr hNameMappings;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpszProgressTitle;

}

[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
static extern int SHFileOperation([In] ref SHFILEOPSTRUCT lpFileOp);
#endregion

private static SHFILEOPSTRUCT _ShFile;
/// <summary>
/// Copies the files from source to target, showing the Progress Dialog
/// </summary>
/// <param name="sSource">Source from where the File(s) will be copied</param>
/// <param name="sTarget">Target or Detination</param>
/// <returns>True or False</returns>
public static void CopyFiles(string sSource, string sTarget)
{

try
{
_ShFile.wFunc = FO_Func.FO_COPY;
_ShFile.fFlags = FO_Func.FOF_ALLOWUNDO;
_ShFile.pFrom = sSource;
_ShFile.pTo = sTarget;
SHFileOperation (ref _ShFile);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
} And in our code we can use FileCopy.CopyFile(@"C:\*.*", @"C:\NewFolder")

wossname
Mar 24th, 2006, 01:29 PM
I havent had time to try this myself (I implemented my own API-free version due to portability issues).

Does this allow you to abort the operation half-way through?

Shuja Ali
Mar 25th, 2006, 05:40 AM
I havent had time to try this myself (I implemented my own API-free version due to portability issues).

Does this allow you to abort the operation half-way through?It is same as Copy using Windows Explorer. You can press cancel button on the progress dialog that is displayed.

aspnetgirl
Aug 2nd, 2006, 02:25 PM
I used your code in my application. When I'm doing copy, the pop-up similar to windows copy pop-up appears and just shows the first file name thru out the operation. And the time remaining seems to be so unreliable, that sometimes it says 5 min and the next second it says 1 min and 10 min after that. Please let me know if this kind of behaviour is expected or something in my code is preventing it from functioning properly.

Shuja Ali
Aug 3rd, 2006, 01:43 AM
Nothing wrong with the code, this is a default Windows behavior. This is how Windows does the copy.

And Welcome to the Forums :wave:

UltraWhack
Oct 6th, 2006, 12:22 PM
Hi Shuja,

Thanks for the code. When I add the class in VC#2005e to use it in C#, I get the error for this codeline
Error 1 Cannot implicitly convert type 'WindowsApplication1.Filecopy.FO_Func' to 'ushort'. An explicit conversion exists (are you missing a cast?)
_ShFile.fFlags = FO_Func.FOF_ALLOWUNDO ;

Any ideas ?

Shuja Ali
Oct 7th, 2006, 05:47 AM
Hi Shuja,

Thanks for the code. When I add the class in VC#2005e to use it in C#, I get the error for this codeline
Error 1 Cannot implicitly convert type 'WindowsApplication1.Filecopy.FO_Func' to 'ushort'. An explicit conversion exists (are you missing a cast?)
_ShFile.fFlags = FO_Func.FOF_ALLOWUNDO ;

Any ideas ?
IN 2005, you don't need to do this. Add a reference to Microsoft.VisualBasic.dll and use new Microsoft.VisualBasic.MyServices.MyServerComputer()
.FileSystem.CopyFile(...);

shakti5385
Oct 7th, 2006, 06:57 AM
Good Code