Unable to access .mdf file after installing application using Visual Studio setup project

£可爱£侵袭症+ 提交于 2020-01-16 07:31:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!