0

I have created a WPF application. I am using Visual Studio setup project to create application setup.

In my application, I have a local database which is located in project folder (Application folder). When I install the application to any other drive than C: drive (operating system drive), then it is working fine. But when I install the application to the C: drive, then my application is not able to access the database file. Also, when I attach the database to SQL Server Management Studio, it is attaching as readonly:

enter image description here

Also, I viewed the Eventviewer and I find this error:

enter image description here

I tried adding the .mdf file to the Programdata folder but still the issue is not resolved. I am aware that it is a permission related issue. But is there any way to resolve this issue using Visual Studio setup project?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Manprit Singh Sahota
  • 1,279
  • 2
  • 14
  • 37
  • A `.mdf` file is a **SQL Server** database file - so in order to use it, you **must have** SQL Server Express installed on that machine. Are you installing this dependency with your application?? – marc_s May 27 '18 at 10:53
  • @marc_s: Yes, I have added prerequisites with the application. Also, I mentioned that I am able to run the application as expected to any other drive other than operating system drive (C drive). – Manprit Singh Sahota May 27 '18 at 13:07

1 Answers1

1

Use Installer class and write below code in Commit method. You have to give the read/write permission to mdf file. You can give this by below line of code; make sure that your installation directory also has the write permission.

string directoryName = @"C:\rnd\ConsoleApplication16\ConsoleApplication16\";
        string SharedCachePath = @"C:\rnd\ConsoleApplication16\ConsoleApplication16\xyz.mdf";
        var fs = File.GetAccessControl(directoryName);
            fs.SetAccessRuleProtection(false, true);
            File.SetAccessControl(SharedCachePath, fs);

you can check here for installer class use Hopefully this will work and resolve your issue.

  • Thanks for the great help. I am able to resolve the issue. I made a small change by providing access the folder containing my Database files as I have separate folder for database files. :) – Manprit Singh Sahota May 27 '18 at 13:56