I am creating a C# WinForms Application along with SQL LocalDB. I am new to SQL. I have also added a setup project to create install the application to other PCs. When I installed the application, it is giving the error:
I do not want user to manually change the permissions and I am not in control of the location whether user will install on C: or any other drive. Is there any way I can update the settings while creating the setup file? The ReadOnly property of the database in Setup is already False.
Is there any other way I can achieve this?
I have checked many posts regarding the same issue which are not working for me:
Unable to update database .MDF is Read Only
Failed to update .mdf database because the database is read-only (Windows application)
Edit: Following @John's solution I added the below code. Please let me know if this is the right way or there is anything more optimized.
var path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\AgeCalculator";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
File.Copy(Directory.GetCurrentDirectory() + "\\dbUserRecords.mdf", path + "\\dbUserRecords.mdf");
}
string conn = Properties.Settings.Default.dbUserRecordsConnectionString;
//Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\dbUserRecords.mdf;Integrated Security=True
conn = conn.Split('|')[0] + path + conn.Split('|')[2];
Edit 2:
I found the better way to do the above Edit :