[英]Unable to access .mdf file after installing application using Visual Studio setup project
I have created a WPF application. 我创建了一个WPF应用程序。 I am using Visual Studio setup project to create application setup. 我正在使用Visual Studio安装程序项目创建应用程序安装程序。
In my application, I have a local database which is located in project folder (Application folder). 在我的应用程序中,我有一个本地数据库,该数据库位于项目文件夹(Application文件夹)中。 When I install the application to any other drive than C: drive (operating system drive), then it is working fine. 当我将应用程序安装到C:驱动器(操作系统驱动器)以外的任何其他驱动器上时,则工作正常。 But when I install the application to the C:
drive, then my application is not able to access the database file. 但是,当我将应用程序安装到C:
驱动器时,我的应用程序将无法访问数据库文件。 Also, when I attach the database to SQL Server Management Studio, it is attaching as readonly: 另外,当我将数据库附加到SQL Server Management Studio时,它以只读方式附加:
Also, I viewed the Eventviewer and I find this error: 另外,我查看了Eventviewer,发现此错误:
I tried adding the .mdf
file to the Programdata
folder but still the issue is not resolved. 我尝试将.mdf
文件添加到Programdata
文件夹,但是仍然无法解决问题。 I am aware that it is a permission related issue. 我知道这是与许可相关的问题。 But is there any way to resolve this issue using Visual Studio setup project? 但是,有什么方法可以使用Visual Studio安装项目解决此问题?
Use Installer class and write below code in Commit method. 使用Installer类,并在Commit方法中编写以下代码。 You have to give the read/write permission to mdf file. 您必须授予对mdf文件的读/写权限。 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. 您可以在此处检查安装程序类的使用情况,希望它可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.