问题
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:
Also, I viewed the Eventviewer and I find this error:
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?
回答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.
来源:https://stackoverflow.com/questions/50550404/unable-to-access-mdf-file-after-installing-application-using-visual-studio-setu