Strange behaviour with Local Accessfile as DB with relativ path

If you are using an Access-File as DB.
And specify it as relative path in your app.config file like this

<add name="MSAccess" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=..\..\AccessFile\LocalAccess.mdb;
User Id=admin;Password=;" providerName="System.Data.OleDb" />
If you are using OpenFileDialog or SaveFileDialog

openFileDialog1.DefaultExt = "pdf";
openFileDialog1.Filter = "Technical paper (*.pdf)|*.pdf";
openFileDialog1.FileName = "";
openFileDialog1.InitialDirectory = lastOpenedPath;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
   // do something
}
Bad Example - No RestoreDirectory, your access-file will not be found after FileDialog...


openFileDialog1.DefaultExt = "pdf";
openFileDialog1.Filter = "Technical paper (*.pdf)|*.pdf";
openFileDialog1.FileName = "";
//HACK PGfader: necessary so that the relative access file is found after this!
openFileDialog1.RestoreDirectory = true;
openFileDialog1.InitialDirectory = lastOpenedPath;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    // do something
}
Good Example - RestoreDirectory used, local relative access file after ShowDialog


RestoreDirectory = false is default necessary to set on OpenFileDialog AND SaveFileDialog

2 comments:

Anonymous said...

I was pulling my hair out trying to figure this out. Thank you very much.

Peter Gfader said...

Hard times, to find the solution to this problem!
How did you realize that it has something to do with the Open- and SaveFileDialog?

Post a Comment

Latest Posts

Popular Posts