Solved

Rename dialog box of file

Posted on 2010-09-20
6
963 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

856 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