Results 1 to 8 of 8

Thread: C#[2003] : Show Copy Progress Dialog While copying Files.

Threaded View

  1. #1

    Thread Starter
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    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:
    1. using System;
    2. using System.Runtime.InteropServices;
    3. public class FileCopy
    4.     {
    5.     #region "API Declaration"
    6.         private enum FO_Func : uint
    7.         {
    8.             FO_MOVE = 0x0001,
    9.             FO_COPY = 0x0002,
    10.             FO_DELETE = 0x0003,
    11.             FO_RENAME = 0x0004,
    12.             FOF_ALLOWUNDO = 0x0040
    13.         }
    14.            
    15.         private struct SHFILEOPSTRUCT
    16.         {
    17.             public IntPtr hwnd;
    18.             public FO_Func wFunc;
    19.             [MarshalAs(UnmanagedType.LPWStr)]
    20.             public string pFrom;
    21.             [MarshalAs(UnmanagedType.LPWStr)]
    22.             public string pTo;
    23.             public ushort fFlags;
    24.             public bool fAnyOperationsAborted;
    25.             public IntPtr hNameMappings;
    26.             [MarshalAs(UnmanagedType.LPWStr)]
    27.             public string lpszProgressTitle;
    28.                
    29.         }
    30.  
    31.         [DllImport("shell32.dll",  CharSet = CharSet.Unicode)]
    32.         static extern int SHFileOperation([In] ref SHFILEOPSTRUCT lpFileOp);
    33.     #endregion
    34.  
    35.         private static SHFILEOPSTRUCT _ShFile;
    36.         /// <summary>
    37.         /// Copies the files from source to target, showing the Progress Dialog
    38.         /// </summary>
    39.         /// <param name="sSource">Source from where the File(s) will be copied</param>
    40.         /// <param name="sTarget">Target or Detination</param>
    41.         /// <returns>True or False</returns>
    42.         public static void CopyFiles(string sSource, string sTarget)
    43.         {
    44.            
    45.             try
    46.             {              
    47.                 _ShFile.wFunc = FO_Func.FO_COPY;
    48.                 _ShFile.fFlags = FO_Func.FOF_ALLOWUNDO;
    49.                 _ShFile.pFrom = sSource;
    50.                 _ShFile.pTo = sTarget;         
    51.                 SHFileOperation (ref _ShFile);
    52.             }
    53.             catch (Exception ex)
    54.             {
    55.                 System.Windows.Forms.MessageBox.Show(ex.Message);              
    56.             }          
    57.         }
    58. }
    And in our code we can use
    VB Code:
    1. 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width