Solved

Rename dialog box of file

Posted on 2010-09-20
6
976 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp.net figure out error 3 45
Datagridview column resizing 8 25
What is needed to become a DBA? 7 51
function that i need to run every 1 min in win form app 6 38
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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 synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

739 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