Link to home
Start Free TrialLog in
Avatar of Steve Williams
Steve WilliamsFlag for United States of America

asked on

How to use Windows 10 System File Copy Dialog for my Windows Form Application.

I'm currently running a Windows 10, version 1903. My IDE is MSVS 2019 Community. I'm creating a Windows Form application using the c# language. I have been trying to use the default windows file copy progress dialog in my windows application. But have had some issues. The files copy as they should but the dialog just doesn't show up reliably. Sometimes it does, sometimes it doesn't. The work flow is as follows:

  • Copy the existing "Default.ivb" file in the destination directory to the archive directory as "replaced_on_2019-07-12_152643_Default" so we don't ever lose that file.
  • The file in the destination directory is then deleted.
  • Then copy the updated "Default.ivb" file from the network share to the destination directory.

I use two different subroutines to complete these tasks, these include:
private void DestToArchive()
        {
            try
            {
                logIt.Message("---STARTED UPDATE---" + " | " + DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss tt" + Environment.NewLine), "log.txt");

                File.SetAttributes(GF.dPath, FileAttributes.Normal);
                Microsoft.VisualBasic.FileIO.FileSystem.CopyFile(GF.dPath, GF.rPath, UIOption.AllDialogs, UICancelOption.DoNothing);
                Application.DoEvents(); //I tried running with and without the do events and it made no difference on either file transfer.

                Thread.Sleep(1000);

                File.Delete(GF.dPath);
                logIt.Message("Moved file: " + GF.dPath + " To: " + GF.rPath + " | " + DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss tt" + Environment.NewLine), "log.txt");
                Thread.Sleep(3000);
            }
            catch (IOException ex)
            {
                //add failure to log.txt
                logIt.Message("---FAILED UPDATE---" + " | " + DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss tt" + Environment.NewLine + ex.ToString() + Environment.NewLine + "----------"), "log.txt");

                string m = "No destination file found" + Environment.NewLine + ex.ToString();
                string c = "UPDATE FAILED!!!!";
                MessageBoxButtons mbb = MessageBoxButtons.YesNo;
                MessageBoxIcon mbi = MessageBoxIcon.Stop;
                DialogResult result = MessageBox.Show(m, c, mbb, mbi);
                if (result == DialogResult.Yes)
                {
                    string lFile = Path.Combine(GF.sDir, "logs\\log.txt");
                    Process.Start(lFile);
                    Application.Exit();
                }
                if (result == DialogResult.No)
                {
                    Application.Exit();
                }
            }
        }

Open in new window

and then
private void ReplaceExistingDefault()
        {
            try
            {
                File.SetAttributes(GF.sPath, FileAttributes.Normal);

                Microsoft.VisualBasic.FileIO.FileSystem.CopyFile(GF.sPath, GF.dPath, UIOption.AllDialogs,UICancelOption.DoNothing);
                Application.DoEvents();


                logIt.Message("Copied file: " + GF.sPath + " To: " + GF.dPath + " | " + DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss tt" + Environment.NewLine), "log.txt");
                Thread.Sleep(3000);
                logIt.Message("---FINISHED UPDATE---" + " | " + DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss tt" + Environment.NewLine), "log.txt");
            }
            catch (IOException ex)
            {
                //add failure to log.txt
                logIt.Message("---FAILED UPDATE---" + " | " + DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss tt" + Environment.NewLine + ex.ToString() + Environment.NewLine + "----------"), "log.txt");

                string m = "No source file found" + Environment.NewLine + ex.ToString();
                string c = "UPDATE FAILED!!!!";
                MessageBoxButtons mbb = MessageBoxButtons.YesNo;
                MessageBoxIcon mbi = MessageBoxIcon.Stop;
                DialogResult result = MessageBox.Show(m, c, mbb, mbi);
                if (result == DialogResult.Yes)
                {
                    string lFile = Path.Combine(GF.sDir, "logs\\log.txt");
                    Process.Start(lFile);
                    Application.Exit();
                }
                if (result == DialogResult.No)
                {
                    Application.Exit();
                }
            }
        }

Open in new window

and then the required usings:
using Microsoft.VisualBasic.FileIO;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows.Forms;

Open in new window

I can't nail down why the dialog doesn't come up but only when it chooses too. The only thing that I can think is that the file size is to small, and it finishes the transfer before the dialog can start.  If this is the case then I will need to come up with a different way to notify the end user that the file completed successfully . The current file size is 282 KB.  Any help on this would be greatly appreciated.
ASKER CERTIFIED SOLUTION
Avatar of Steve Williams
Steve Williams
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial