Displaying the Windows Copy Dialog Box when Copying Files Through .NET


As you probably know, .NET is quite powerful when it comes to any file operations. This is because of the System.IO Namespace, which is mostly all you need for any file operation. There are times, though, that you need a bit more. This could be an added personal touch, or a proper Windows look and feel. Today, I will show you how to display the Windows Copy Dialog box whilst copying files through .NET.


Open Visual Studio and create either a C# or a VB.NET Windows Forms application. After the form has been created, add a button and give it a Text property of ‘Copy’. You may name your objects (the form and the button) anything you desire, but keep in mind that my names might differ from yours.

Add a new class and add the necessary Namespaces.


using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;


Imports System.Runtime.InteropServices

These Namespaces will allow you to work with the Windows API in a managed way. Add the File Operation Enumeration.


   private enum FO_Func : uint
      FO_COPY = 0x0002,
      FO_DELETE = 0x0003,
      FO_MOVE = 0x0001,
      FO_RENAME = 0x0004,


   Private Enum FO_Func As Short

      FO_COPY = &H2

      FO_DELETE = &H3

      FO_MOVE = &H1

      FO_RENAME = &H4

   End Enum

Add the remaining API Structure and Function Declarations.


   private struct SHFILEOPSTRUCT
      public IntPtr hwnd;
      public FO_Func wFunc;
      public string pFrom;
      public string pTo;
      public ushort fFlags;
      public bool fAnyOperationsAborted;
      public IntPtr hNameMappings;
      public string lpszProgressTitle;


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

   private static SHFILEOPSTRUCT _ShFile;


   Private Structure SHFILEOPSTRUCT

      Public hwnd As IntPtr
      Public wFunc As FO_Func
      Public pFrom As String
      Public pTo As String
      Public fFlags As UShort
      Public fAnyOperationsAborted As Boolean
      Public hNameMappings As IntPtr
      Public lpszProgressTitle As String

   End Structure

   <DllImport("shell32.dll", CharSet:=CharSet.Unicode)>
   Private Shared Function SHFileOperation(
      <[In]> ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
   End Function

   Private Shared _ShFile As SHFILEOPSTRUCT

The SHFILEOPSTRUCT API copies, moves, renames, or deletes a file system object. Add the CopyFiles sub procedure.


   public static void CopyFiles(string sSource, string sTarget)

         _ShFile.wFunc = FO_Func.FO_COPY;
         _ShFile.pFrom = sSource;
         _ShFile.pTo = sTarget;
         SHFileOperation(ref _ShFile);
      catch (Exception ex)


   Public Shared Sub CopyFiles(ByVal sSource As String, _
         ByVal sTarget As String)


         _ShFile.wFunc = FO_Func.FO_COPY
         _ShFile.pFrom = sSource
         _ShFile.pTo = sTarget

      Catch ex As Exception


      End Try

   End Sub

The CopyFiles sub procedure makes use of the API methods and objects we created earlier to do the physical copy as well as to display the Windows Copy dialog box whilst doing so.

Add a call to this sub procedure in your Button’s Click event.


   private void button1_Click(object sender, EventArgs e)

      Copy.CopyFiles(@"C:\*.*", @"C:\NewFolder");



   Private Sub button1_Click(sender As Object, e As EventArgs) _
         Handles button1.Click

      Copy.CopyFiles("C:\*.*", "C:\NewFolder")

   End Sub

The preceding code copies all folders and files to a folder named NewFolder on the C: drive. Following are a few pictures that might appear while running this project.

Create New Folder?
Figure 1: Create New Folder?

Discovery of items
Figure 2: Discovery of items

Access Denied
Figure 3: Access Denied


It is small things that make a big difference in your applications. Users expect a certain look and feel of certain operations. Incorporating the Windows look and feel into file operations will make your users appreciate your applications much more.

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read