Results 1 to 8 of 8

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

  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

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  3. #3

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

    Re: C# : Show Copy Progress Dialog While copying Files.

    Quote 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

  4. #4
    New Member
    Join Date
    Aug 2006
    Posts
    1

    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.

  5. #5

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

    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

  6. #6
    New Member
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    3

    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 ?

  7. #7

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

    Re: C# : Show Copy Progress Dialog While copying Files.

    Quote 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

  8. #8
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

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

    Good Code

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