Solved

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

Posted on 2014-01-26
4
237 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 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now