Basically there is what you should do:
Do not leave the database file in your project folder, copy it elsewhere, like to Environment.SpecialFolder.ApplicationData
. You do it when the app is first launched and there no database file exists, so you copy the database from your project folder to your data folder.
There is quite a good answer, how to deal with the problem here.
If you need to write to the data deployed with your executable you
should first copy it someplace you know the user will be able to write
to, such as to Environment.SpecialFolder.ApplicationData, and write to
the copy. Not only is DataDirectory not necessarily writable by users,
it is part of the deployment and not part of the user data; if you
repair or uninstall your executable then DataDirectory gets
reinstalled or deleted. Users don't like it when you delete their
data, so don't save it to DataDirectory
You can get the path of your ClickOnce executable via this command:
var exePath = System.IO.Path.GetDirectoryName(
new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath);
See then this answer:
Use something like that if the database should be used by multiple users of target computer:
AppDomain.CurrentDomain.SetData("DataDirectory", Path.Combine(System.Environment.GetEnvironmentVariable("public"), YOUR_FOLDER_NAME));
Or just one database for one user:
AppDomain.CurrentDomain.SetData("DataDirectory", Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),YOUR_FOLDER_NAME));
You should be aware that ClickOnce deployment method allows users that are not local admins to install your app, so it is better to choose such a folder on the target machine that should be by default writable for the end user.
Then you use |DataDirectory|
in the connection string within your config file, like in the mentioned answer.
The DataDirectory would be a place where you copy the mdb file from exePath to your target data folder.
Your problem also is, that ClickOnce apps are installed in the user profile, so the database, if it is to be shared among different users of 1 computer, should be placed somewhere in c:\users\public
folder. If it is to be used only by a single user, it can be placed in documents folder or somewhere in the current user's profile.
So for a shared database your db folder where you copy your mdb after the first deployment could be something like this:
Path.Combine(System.Environment.GetEnvironmentVariable("public"), YOUR_FOLDER_NAME)
for a non-shared database it would be
Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),YOUR_FOLDER_NAME)
There is usually a very good reason not to leave your data in your project folder as it is quite plainly recommended by Microsoft here:
When a ClickOnce application is uninstalled, its Data Directory is
also removed. Never use the Data Directory to store end-user–managed
data, such as documents.
But if it is OK with you then you might be just fine changing the file attributes in Visual Studio, see this answer.
But also when you udpate the mdb file within your project, the locally installed data file gets rewritten by the published ClickOnce updates - one more link here, just to see that this is quite common approach to have data copied elsewhere by ClickOnce app at startup.