My application reads an Access database file that can be updated at runtime by downloading a replacement MDB file (with the same schema) and overwriting the existing file. This is all well and good during development, but it breaks during deployment.
The main problem is that the connection string for the data adapters is
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\ModelDatabase.mdb"
in which the |DataDirectory| token often equals the application root directory. Vista doesn't like it when I download a new file and try to delete the existing MDB file -- an UnauthorizedAccessException
is thrown, and rightfully so, because I shouldn't be messing with files in the application root.
Instead, I should be downloading and updating files in an application data directory, such as Application.CommonAppDataPath
.
So, I am faced with two possible solutions (in order of preference):
Somehow redefine that |DataDirectory| token to point to
Application.CommonAppDataPath
. I've triedAppDomain.CurrentDomain.SetData("DataDirectory", Application.CommonAppDataPath)
with no luck; the data adapter throws an exception "Invalid value for key 'data source'." when it tries to populate a DataSet.Update the dataset programmatically, populating every table using its specific data adapter, then inserting, updating, and deleting data rows. This seems like an awful pain when a simple file move would suffice.
Is there a way to make solution #1 work, by redefining this magic DataDirectory token? Where is this domain variable defined, anyway?
Or is there a better way to solve this problem?