2

I have a C# app which reads from and writes to an Access database. There is one database file per user. My intention is to check for the existence of the MDB in the user's My Documents folder at launch, and if the MDB isn't found, then copy the template MDB to that folder.

I have already added the template MDB to my project and placed it in its own folder which I have called Packaged. However, I am unable to refer to this Packaged folder from code as it doesn't appear in IntelliSense.

My intention had been to use File.Copy to copy the MDB over, but I cannot determine the file path as I can't access the MDB in code. And presumably it wouldn't have a file path anyway if it's just packaged in the .exe?

What would be the best way to achieve this, given that I would rather not distribute a separate MDB if possible?

DavidRR
  • 18,291
  • 25
  • 109
  • 191
Niall
  • 1,551
  • 4
  • 23
  • 40

2 Answers2

2

I've done this in the past by making the MDB an embedded resource, and writing it to disk as needed (if it doesn't exist).

Here is more info on writing an embedded resource to file.

Community
  • 1
  • 1
Jon B
  • 51,025
  • 31
  • 133
  • 161
  • I have gone with your suggestion and it's working fine, many thanks – Niall Jan 09 '13 at 18:34
  • Some might find the Q&A for [Write file from assembly resource stream to disk](http://stackoverflow.com/q/864140/1497596) more informative. – DavidRR Nov 13 '15 at 14:58
1

You'd almost have to use one of the Environmental paths for the template MDB. Like ApplicationData or Documents and Settings\Username\Local Settings\ or one of the other ones. Local User Data is the best way for user specific data. IN the code in the beginning, determine to see if the file exists in the first run. If it exists copy the template, if not don't.

That way the user has full read and write access and the ability to copy the file or duplicate the file without security problems. These environmental variables are accessible through

  Environment.GetEnvironmentVariable  

You could also have a registry setting and read and write to the registry for that specific application, that has a simple DatabaseAvailable key, and toggle it yes or no.

You could also embed the MDB as a resource too, and then write it as necessary.

apollosoftware.org
  • 12,161
  • 4
  • 48
  • 69