Solved

Rename dialog box of file

Posted on 2010-09-20
6
930 Views
Last Modified: 2013-12-16
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

0
Comment
Question by:ANINDYA
  • 3
  • 2
6 Comments
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33716894
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
 
LVL 5

Expert Comment

by:Vipul Patel
ID: 33719042
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
 

Author Comment

by:ANINDYA
ID: 33722333
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:ANINDYA
ID: 33722348
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
 
LVL 5

Accepted Solution

by:
Vipul Patel earned 500 total points
ID: 33727378
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
 

Author Closing Comment

by:ANINDYA
ID: 33731734
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now