dcsimg
 

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

Thursday Nov 1st 2018 by Hannes DuPreez

Learn to display the Windows Copy Dialog box whilst copying files through .NET. Examples are in C# and VB.NET.

Introduction

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.

Practical

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.

C#

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

VB.NET

Imports System.Runtime.InteropServices

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

C#

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

VB.NET

   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.

C#

   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);

   private static SHFILEOPSTRUCT _ShFile;

VB.NET

   Private Structure SHFILEOPSTRUCT

      Public hwnd As IntPtr
      Public wFunc As FO_Func
      <MarshalAs(UnmanagedType.LPWStr)>
      Public pFrom As String
      <MarshalAs(UnmanagedType.LPWStr)>
      Public pTo As String
      Public fFlags As UShort
      Public fAnyOperationsAborted As Boolean
      Public hNameMappings As IntPtr
      <MarshalAs(UnmanagedType.LPWStr)>
      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.

C#

   public static void CopyFiles(string sSource, string sTarget)
   {

      try
      {
         _ShFile.wFunc = FO_Func.FO_COPY;
         _ShFile.pFrom = sSource;
         _ShFile.pTo = sTarget;
         SHFileOperation(ref _ShFile);
      }
      catch (Exception ex)
      {
         MessageBox.Show(ex.Message);
      }
   }

VB.NET

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

      Try

         _ShFile.wFunc = FO_Func.FO_COPY
         _ShFile.pFrom = sSource
         _ShFile.pTo = sTarget
         SHFileOperation(_ShFile)

      Catch ex As Exception

         MessageBox.Show(ex.Message)

      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.

C#

   private void button1_Click(object sender, EventArgs e)
   {

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

   }

VB.NET

   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

Conclusion

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.

Home
Mobile Site | Full Site