?
Solved

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

Posted on 2014-01-26
4
Medium Priority
?
256 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
  • 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Integration Management Part 2
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month17 days, 10 hours left to enroll

830 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