Solved

Rename dialog box of file

Posted on 2010-09-20
6
972 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JQuery Detecting page post back 6 19
How to repeat the data 4 30
T-SQL and CLR parameter strings 9 23
Using sample Autorize.net c# simple example 1 22
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

756 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