?
Solved

Save file to location with the SaveFileDialog but the file allready exists!

Posted on 2014-01-26
4
Medium Priority
?
252 Views
Last Modified: 2014-01-26
Hi,

What can I do to save a file to a certain location with the SaveFileDialog but the same file allready exists! I get an message to override it. And I click Yes. But then I get an error:
   
System.IO.IOExeption: the file c:\Users\Peter\Desktop\DBRoot.bak already exists.

        private void bbBackup_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string pathDB = Application.StartupPath + "\\DBRoot.mdb";
            glucsTA.Update(glucsDS);
            glucsDS.AcceptChanges();
            suppTA.Update(suppDS);
            suppDS.AcceptChanges();
            favorTA.Update(favorDS);
            favorDS.AcceptChanges();
            calenTA.Update(calenDS);
            calenDS.AcceptChanges();
            SaveFileDialog saveFileDlg = new SaveFileDialog();
            saveFileDlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            saveFileDlg.Filter = "Backup File (*.bak)|*.bak";
            saveFileDlg.FileName = "DBRoot.bak";
            try
            {
                if (saveFileDlg.ShowDialog() == DialogResult.OK)
                {
                    System.IO.File.Copy(pathDB, saveFileDlg.FileName);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString());
            }
        }

Open in new window


Greetings, Peter Kiers
0
Comment
Question by:peterkiers
[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
  • 2
4 Comments
 
LVL 14

Expert Comment

by:Matti
ID: 39810261
use date/time in the default file name then it does not exist.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 39810262
I don't understand could you help me?
Greetings, Peter
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 2000 total points
ID: 39810376
The role of the SaveFileDialog is to select a file. It flags the user if the file already exists, but does nothing else. It's you job to react.

It's your Copy that causes the problem. The way you call it, overwrite is not enabled.

Try passing true as a third parameter to Copy. This enables overwriting the file.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 39810584
Ah, yes indeed James your right. Thank for the solution.
Greetings, Peter Kiers
0

Featured Post

Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

777 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