none
C# How can I copy files from one folder to another with prompt to overwrite if file exists? RRS feed

  • Question



  • I'm writing a C# .NET Winforms project that uses an OpenFileDialog to copy files from one folder to another. Here's the code...        

    private void tsbtnMediaPool_Click(object sender, EventArgs e)
            {
                if (tsbtnMediaPool.Enabled == false)
                {
                    MessageBox.Show("Project must be saved before Media Pool can be used.", "oops.",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    OpenFileDialog ofd = new OpenFileDialog();
                    ofd.Title = "Media Pool...";
                    if (Properties.Settings.Default.blOpen2ProjectMedia)
                        ofd.InitialDirectory = glbstrMediaPoolPath;
                    else
                        ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                    ofd.CheckFileExists = true;
                    ofd.CheckPathExists = true;
                    ofd.Filter = "Media Items (*.bmp, *.jpg, *.jpeg, *.png, *.gif, *.mp3, *.wma)|*.bmp; *.jpg; *.jpeg; *.png; *.gif; *.mp3; *.wma";
                    ofd.Multiselect = true;
                    if (DialogResult.OK == ofd.ShowDialog())
                    {
                        if (ofd.InitialDirectory != glbstrMediaPoolPath)
                        {
                            foreach (string path in ofd.FileNames)
                            {
                                string destpath = glbstrMediaPoolPath + Path.GetFileName(path);
                                if (File.Exists(destpath))
                                {
                                    if (DialogResult.Yes == MessageBox.Show(Path.GetFileName(destpath) + " exists. Replace?", "File Exists", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                                        File.Copy(path, destpath, true);
                                }
                                else
                                    File.Copy(path, destpath);
                            }
                        }
                        //else if (ofd.InitialDirectory == glbstrMediaPoolPath)
                        //MessageBox.Show("Source file path and destination file path are the same.", "oops.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
    
                    ofd.Dispose();
                }
            }


    The problem is that I can't get the message box code for asking to overwrite the file if it exists to work. No message box appears on the screen.  How can I make that happen? Should I even use an OpenFileDialog for this?
    Sunday, February 25, 2018 1:15 AM

Answers



  • I'm writing a C# .NET Winforms project that uses an OpenFileDialog to copy files from one folder to another. Here's the code...        

                        if (ofd.InitialDirectory != glbstrMediaPoolPath)
                        {
                            foreach (string path in ofd.FileNames)
                            {
                                string destpath = glbstrMediaPoolPath + Path.GetFileName(path);
                                if (File.Exists(destpath))
                                {
                                    if (DialogResult.Yes == MessageBox.Show(Path.GetFileName(destpath) + " exists. Replace?", "File Exists", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                                        File.Copy(path, destpath, true);
                                }
                                else
                                    File.Copy(path, destpath);
                            }
    

    The problem is that I can't get the message box code for asking to overwrite the file if it exists to work. No message box appears on the screen.  How can I make that happen?

    The problem does not appear to be with the code you have posted for
    checking for an existing file and displaying a messagebox if it does.

    So I suggest that you need to do some debugging - set a breakpoint at

    foreach (string path in ofd.FileNames)

    and then start stepping through the code. Check the contents of the
    variables as you go. Note especially the contents of destpath and
    ensure that it is correct.

    By way of testing a minimal example from the code you posted, I removed
    some of the initial tests and hardcoded a destination path. Placed in a
    button click event:

    private void button1_Click(object sender, EventArgs e)
    {
        string glbstrMediaPoolPath = "c:\\temp\\"; // ***
    
        //if (tsbtnMediaPool.Enabled == false)
        //{
        //    MessageBox.Show("Project must be saved before Media Pool can be used.", "oops.",
        //                    MessageBoxButtons.OK, MessageBoxIcon.Error);
        //}
        //else
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Media Pool...";
            //if (Properties.Settings.Default.blOpen2ProjectMedia)
            //    ofd.InitialDirectory = glbstrMediaPoolPath;
            //else
                ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            ofd.CheckFileExists = true;
            ofd.CheckPathExists = true;
            ofd.Filter = "Media Items (*.bmp, *.jpg, *.jpeg, *.png, *.gif, *.mp3, *.wma)|*.bmp; *.jpg; *.jpeg; *.png; *.gif; *.mp3; *.wma";
            ofd.Multiselect = true;
            if (DialogResult.OK == ofd.ShowDialog())
            {
                if (ofd.InitialDirectory != glbstrMediaPoolPath)
                {
                    foreach (string path in ofd.FileNames)
                    {
                        string destpath = glbstrMediaPoolPath + Path.GetFileName(path);
                        if (File.Exists(destpath))
                        {
                            if (DialogResult.Yes == MessageBox.Show(Path.GetFileName(destpath) + " exists. Replace?", "File Exists", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                                File.Copy(path, destpath, true);
                        }
                        else
                            File.Copy(path, destpath);
                    }
                }
                //else if (ofd.InitialDirectory == glbstrMediaPoolPath)
                //MessageBox.Show("Source file path and destination file path are the same.", "oops.", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
    
            ofd.Dispose();
        }
    }
    

    I selected two files to be copied - both copied OK with no messagebox
    on the first pass through. I then repeated the same steps selecting the
    same files and the program displayed the messagebox for each existing file:

    - Wayne

    • Proposed as answer by Fei HuModerator Sunday, February 25, 2018 6:29 AM
    • Marked as answer by WikiGrrrl Sunday, February 25, 2018 6:35 PM
    Sunday, February 25, 2018 6:26 AM

All replies

  • Hello WikiGrrrl,

    You code seems that works well on my side, I just change some variables value from your code. Here is my running result.

    Maybe something is different between us. If the error still exists, you could give me more detailed info about your code, like variables value and operation.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, February 25, 2018 6:07 AM
    Moderator


  • I'm writing a C# .NET Winforms project that uses an OpenFileDialog to copy files from one folder to another. Here's the code...        

                        if (ofd.InitialDirectory != glbstrMediaPoolPath)
                        {
                            foreach (string path in ofd.FileNames)
                            {
                                string destpath = glbstrMediaPoolPath + Path.GetFileName(path);
                                if (File.Exists(destpath))
                                {
                                    if (DialogResult.Yes == MessageBox.Show(Path.GetFileName(destpath) + " exists. Replace?", "File Exists", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                                        File.Copy(path, destpath, true);
                                }
                                else
                                    File.Copy(path, destpath);
                            }
    

    The problem is that I can't get the message box code for asking to overwrite the file if it exists to work. No message box appears on the screen.  How can I make that happen?

    The problem does not appear to be with the code you have posted for
    checking for an existing file and displaying a messagebox if it does.

    So I suggest that you need to do some debugging - set a breakpoint at

    foreach (string path in ofd.FileNames)

    and then start stepping through the code. Check the contents of the
    variables as you go. Note especially the contents of destpath and
    ensure that it is correct.

    By way of testing a minimal example from the code you posted, I removed
    some of the initial tests and hardcoded a destination path. Placed in a
    button click event:

    private void button1_Click(object sender, EventArgs e)
    {
        string glbstrMediaPoolPath = "c:\\temp\\"; // ***
    
        //if (tsbtnMediaPool.Enabled == false)
        //{
        //    MessageBox.Show("Project must be saved before Media Pool can be used.", "oops.",
        //                    MessageBoxButtons.OK, MessageBoxIcon.Error);
        //}
        //else
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Media Pool...";
            //if (Properties.Settings.Default.blOpen2ProjectMedia)
            //    ofd.InitialDirectory = glbstrMediaPoolPath;
            //else
                ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            ofd.CheckFileExists = true;
            ofd.CheckPathExists = true;
            ofd.Filter = "Media Items (*.bmp, *.jpg, *.jpeg, *.png, *.gif, *.mp3, *.wma)|*.bmp; *.jpg; *.jpeg; *.png; *.gif; *.mp3; *.wma";
            ofd.Multiselect = true;
            if (DialogResult.OK == ofd.ShowDialog())
            {
                if (ofd.InitialDirectory != glbstrMediaPoolPath)
                {
                    foreach (string path in ofd.FileNames)
                    {
                        string destpath = glbstrMediaPoolPath + Path.GetFileName(path);
                        if (File.Exists(destpath))
                        {
                            if (DialogResult.Yes == MessageBox.Show(Path.GetFileName(destpath) + " exists. Replace?", "File Exists", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                                File.Copy(path, destpath, true);
                        }
                        else
                            File.Copy(path, destpath);
                    }
                }
                //else if (ofd.InitialDirectory == glbstrMediaPoolPath)
                //MessageBox.Show("Source file path and destination file path are the same.", "oops.", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
    
            ofd.Dispose();
        }
    }
    

    I selected two files to be copied - both copied OK with no messagebox
    on the first pass through. I then repeated the same steps selecting the
    same files and the program displayed the messagebox for each existing file:

    - Wayne

    • Proposed as answer by Fei HuModerator Sunday, February 25, 2018 6:29 AM
    • Marked as answer by WikiGrrrl Sunday, February 25, 2018 6:35 PM
    Sunday, February 25, 2018 6:26 AM
  • Thanks for your response, Wayne. I tried setting breakpoints and "path" and "destpath" have the values they're supposed to have. I saved the solution, closed VS 2017 and reopened the solution and now, for some reason, it's working the way it should. I'll mark this question as answered. Thanks for your help!
    Sunday, February 25, 2018 6:35 PM