Rename dialog box of file

Experts
Please see the following code
using (OpenFileDialog srcDialog = new OpenFileDialog())
            {
                srcDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                //srcDialog.Filter = "jpg files (*.jpg)|*.jpg ";
                srcDialog.FilterIndex = 2;
                srcDialog.RestoreDirectory = true;
               
               

                if (srcDialog.ShowDialog() == DialogResult.OK)
                {
                    using (FolderBrowserDialog destDir = new FolderBrowserDialog())
                    {
                       
                        if (destDir.ShowDialog() == DialogResult.OK)
                        {
                            string filename = srcDialog.FileName;
                            string destinationFolder = destDir.SelectedPath;

                            if (File.Exists(filename) && Directory.Exists(destinationFolder))
                            {
                                DialogResult dlg = MessageBox.Show("There is a file with the same name.\nDo you want to replace the existing File?","Replace the Existing File?",MessageBoxButtons.OKCancel,MessageBoxIcon.Exclamation);
                                if (dlg == DialogResult.OK)
                                {
                                    destinationFolder += "\\" + Path.GetFileName(filename);
                                    File.Copy(filename, destinationFolder, true);  // Change the last param to true if you wish to overwrite.
                                    MessageBox.Show(destinationFolder.ToString());
                                }
                                else if(dlg==DialogResult.Cancel)
                                {
                                    destinationFolder += "\\" + Path.GetFileName(filename);
                                    File.Copy(filename, destinationFolder, false);  // Change the last param to true if you wish to overwrite.
                                    MessageBox.Show(destinationFolder.ToString());
                                }
                            }
                        }
                    }
                }
            }


Here I want a dialog box should appear when there is already a file of the same name exists.
after the statement
else if(dlg==DialogResult.Cancel)

Thanking you

using (OpenFileDialog srcDialog = new OpenFileDialog())
            {
                srcDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                //srcDialog.Filter = "jpg files (*.jpg)|*.jpg ";
                srcDialog.FilterIndex = 2;
                srcDialog.RestoreDirectory = true;
                
                

                if (srcDialog.ShowDialog() == DialogResult.OK)
                {
                    using (FolderBrowserDialog destDir = new FolderBrowserDialog())
                    {
                        
                        if (destDir.ShowDialog() == DialogResult.OK)
                        {
                            string filename = srcDialog.FileName;
                            string destinationFolder = destDir.SelectedPath;

                            if (File.Exists(filename) && Directory.Exists(destinationFolder))
                            {
                                DialogResult dlg = MessageBox.Show("There is a file with the same name.\nDo you want to replace the existing File?","Replace the Existing File?",MessageBoxButtons.OKCancel,MessageBoxIcon.Exclamation);
                                if (dlg == DialogResult.OK)
                                {
                                    destinationFolder += "\\" + Path.GetFileName(filename);
                                    File.Copy(filename, destinationFolder, true);  // Change the last param to true if you wish to overwrite.
                                    MessageBox.Show(destinationFolder.ToString());
                                }
                                else if(dlg==DialogResult.Cancel)
                                {
                                    destinationFolder += "\\" + Path.GetFileName(filename);
                                    File.Copy(filename, destinationFolder, false);  // Change the last param to true if you wish to overwrite.
                                    MessageBox.Show(destinationFolder.ToString());
                                }
                            }
                        }
                    }
                }
            }

Open in new window

ANINDYAAsked:
Who is Participating?
 
Vipul Patel.NET ExpertCommented:
Hi Anindya,

Thanks for pointing the error.

I have changed the code please try it and let me know if still problem exists.
private void button1_Click(object sender, EventArgs e)
        {
        changeFile:
            using (System.Windows.Forms.OpenFileDialog srcDialog = new System.Windows.Forms.OpenFileDialog())
            {
                srcDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                //srcDialog.Filter = "jpg files (*.jpg)|*.jpg ";
                srcDialog.FilterIndex = 2;
                srcDialog.RestoreDirectory = true;

                if (srcDialog.ShowDialog() == DialogResult.OK)
                {
                    string srcFileName = srcDialog.FileName;
                changeFolder:
                    using (FolderBrowserDialog destDir = new FolderBrowserDialog())
                    {

                        if (destDir.ShowDialog() == DialogResult.OK)
                        {
                            string destFile = destDir.SelectedPath + "\\" + System.IO.Path.GetFileName(srcFileName);
                            string destinationFolder = destDir.SelectedPath;

                            if (File.Exists(destFile))
                            {
                                DialogResult dlg = MessageBox.Show("There is a file with the same name.\nDo you want to replace the existing File?", "Replace the Existing File?", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
                                if (dlg == DialogResult.OK)
                                {
                                    destinationFolder += "\\" + Path.GetFileName(srcFileName);
                                    File.Copy(srcFileName, destFile, true);  // Change the last param to true if you wish to overwrite.
                                    MessageBox.Show(destinationFolder.ToString());
                                }
                                else if (dlg == DialogResult.Cancel)
                                {
                                    DialogResult changeresult = MessageBox.Show("Do you want to change destination folder?", "Change", MessageBoxButtons.YesNoCancel);
                                    destDir.Dispose();
                                    if (changeresult == System.Windows.Forms.DialogResult.Yes)
                                    {
                                        goto changeFolder;
                                    }
                                    else if (changeresult == System.Windows.Forms.DialogResult.No)
                                    {
                                        srcDialog.Dispose();
                                        goto changeFile;
                                    }
                                }
                            }
                            else
                            {
                                File.Copy(srcFileName, destFile, false);  // Change the last param to true if you wish to overwrite.
                            }
                        }
                    }
                }
            }
        }

Open in new window

0
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
Hi,

Please check code. However I would like to know reason of checking same code again.

Regards,
VSS

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace OpenFileDialog
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public string CommandID
        {
            get
            {
                return CommandID;
            }
            set
            {
                CommandID = "<COMMANDID>" + value + "</COMMANDID>";
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            using (System.Windows.Forms.OpenFileDialog srcDialog = new System.Windows.Forms.OpenFileDialog())
            {
                srcDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                //srcDialog.Filter = "jpg files (*.jpg)|*.jpg ";
                srcDialog.FilterIndex = 2;
                srcDialog.RestoreDirectory = true;

                if (srcDialog.ShowDialog() == DialogResult.OK)
                {
                    using (FolderBrowserDialog destDir = new FolderBrowserDialog())
                    {

                        if (destDir.ShowDialog() == DialogResult.OK)
                        {
                            string filename = srcDialog.FileName;
                            string destinationFolder = destDir.SelectedPath;

                            if (File.Exists(filename) && Directory.Exists(destinationFolder))
                            {
                                DialogResult dlg = MessageBox.Show("There is a file with the same name.\nDo you want to replace the existing File?", "Replace the Existing File?", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
                                if (dlg == DialogResult.OK)
                                {
                                    destinationFolder += "\\" + Path.GetFileName(filename);
                                    File.Copy(filename, destinationFolder, true);  // Change the last param to true if you wish to overwrite.
                                    MessageBox.Show(destinationFolder.ToString());                                   
                                }
                                else if (dlg == DialogResult.Cancel)
                                {
                                    if (File.Exists(filename) && Directory.Exists(destinationFolder))
                                    {
                                        MyDialog dialog = new MyDialog();
                                        dialog.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                                        dialog.Size = new Size(100, 100);
                                        dialog.MaximizeBox = false;
                                        dialog.MinimizeBox = false;
                                        dialog.ShowDialog();
                                    }

                                    destinationFolder += "\\" + Path.GetFileName(filename);
                                    File.Copy(filename, destinationFolder, false);  // Change the last param to true if you wish to overwrite.
                                    MessageBox.Show(destinationFolder.ToString());
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    class MyDialog : Form
    {
        Label lbl;
        public MyDialog()
        {
            lbl = new Label();
            lbl.Text = "File Already Exists";
            Controls.Add(lbl);
        }
    }
}

Open in new window

0
 
Vipul Patel.NET ExpertCommented:
I think the flow should be like this;

1. User selects a file X.
2. User selects a folder Y having same file name X.
3. Message is displayed to replace the existing file.
4. If Ok is pressed then file is replaced.
5. If Cancel is pressed then message is displayed to change about changing destination folder.
6. If Yes is selected then system again opens the folder browser dialog to change the destination folder.
7. If No is selected then system again opens the file browser dialog to select the file and then folder browser dialog so on..

the modified code is attached.

It might be helpful.

private void button1_Click(object sender, EventArgs e)
        {
            changeFile:
            using (System.Windows.Forms.OpenFileDialog srcDialog = new System.Windows.Forms.OpenFileDialog())
            {
                srcDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                //srcDialog.Filter = "jpg files (*.jpg)|*.jpg ";
                srcDialog.FilterIndex = 2;
                srcDialog.RestoreDirectory = true;

                if (srcDialog.ShowDialog() == DialogResult.OK)
                {
                changeFolder:
                    using (FolderBrowserDialog destDir = new FolderBrowserDialog())
                    {

                        if (destDir.ShowDialog() == DialogResult.OK)
                        {
                            string filename = srcDialog.FileName;
                            string destinationFolder = destDir.SelectedPath;

                            if (File.Exists(filename) && Directory.Exists(destinationFolder))
                            {
                                DialogResult dlg = MessageBox.Show("There is a file with the same name.\nDo you want to replace the existing File?", "Replace the Existing File?", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
                                if (dlg == DialogResult.OK)
                                {
                                    destinationFolder += "\\" + Path.GetFileName(filename);
                                    File.Copy(filename, destinationFolder, true);  // Change the last param to true if you wish to overwrite.
                                    MessageBox.Show(destinationFolder.ToString());
                                }
                                else if (dlg == DialogResult.Cancel)
                                {
                                    DialogResult changeresult=  MessageBox.Show("Do you want to change destination folder?", "Change", MessageBoxButtons.YesNo);
                                    destDir.Dispose();
                                    if (changeresult == System.Windows.Forms.DialogResult.Yes)
                                    {
                                        goto changeFolder;
                                    }
                                    else
                                    {
                                        srcDialog.Dispose();
                                        goto changeFile;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

Open in new window

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
ANINDYAAuthor Commented:
Expert vs00saini
Thanks for reply.
please see the image which I have attached.
there is an error particularly at that time when a file exists in the destination folder and I am trying to sava it.
thanking you
anindya
error.JPG
0
 
ANINDYAAuthor Commented:
Expert vrluckyin
Thanks for the reply .
Sir your code is working fine .
But only one problem is coming that is when saving the file in a different destination folder with same name then the message is coming as
there exists a file by the same name
this message should not come .
Sir would you please like to see it again.
Thanking you
anindya
0
 
ANINDYAAuthor Commented:
Expert vrluckyin
Thanks a lot for answering to my doubts.
Your code is working fine.
I am sorry for answering you late as I am from India and you may be from USA .So it is night by that time when you post your revised code to me.

Expert vrluckyin I have a small request I have posted a question on EE .If you have a trifle bit of time you may glance upon it .
The url is

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26490572.html

Thanks for reply .
Hope to get your assistance in future too.
Thanking you
Anindya Chatterjee
Bangalore
India
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.