-
Feb 20th, 2006, 07:08 AM
#1
Thread Starter
Shared Member
C#[2003] : Show Copy Progress Dialog While copying Files.
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
VB Code:
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
VB Code:
FileCopy.CopyFile(@"C:\*.*", @"C:\NewFolder")
Last edited by Shuja Ali; Oct 7th, 2006 at 05:38 AM.
Reason: Added the version number
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Mar 24th, 2006, 02:29 PM
#2
Re: C# : Show Copy Progress Dialog While copying Files.
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?
I don't live here any more.
-
Mar 25th, 2006, 06:40 AM
#3
Thread Starter
Shared Member
Re: C# : Show Copy Progress Dialog While copying Files.
Originally Posted by wossname
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.
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Aug 2nd, 2006, 02:25 PM
#4
New Member
Re: C# : Show Copy Progress Dialog While copying Files.
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.
-
Aug 3rd, 2006, 01:43 AM
#5
Thread Starter
Shared Member
Re: C# : Show Copy Progress Dialog While copying Files.
Nothing wrong with the code, this is a default Windows behavior. This is how Windows does the copy.
And Welcome to the Forums
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Oct 6th, 2006, 12:22 PM
#6
New Member
Re: C# : Show Copy Progress Dialog While copying Files.
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 ?
-
Oct 7th, 2006, 05:47 AM
#7
Thread Starter
Shared Member
Re: C# : Show Copy Progress Dialog While copying Files.
Originally Posted by UltraWhack
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
Code:
new Microsoft.VisualBasic.MyServices.MyServerComputer()
.FileSystem.CopyFile(...);
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Oct 7th, 2006, 06:57 AM
#8
Re: C#[2003] : Show Copy Progress Dialog While copying Files.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|