简体   繁体   English

使用Visual Studio安装程序安装应用程序后无法访问.mdf文件

[英]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.

相关问题 在 Visual Studio 中重命名安装项目中的文件 - Renaming file in setup project in visual studio 无法访问桌面应用程序中的 mdf 文件 - Can't access mdf file in desktop application 从Visual Studio 2010创建.MDF文件的问题 - Problem with creating .MDF file from Visual Studio 2010 Visual Studio 2012安装程序项目模板在哪里? - Where is Visual studio 2012 Setup project template? 在Visual Studio 2012中创建WPF安装程序项目 - Create WPF Setup project in visual studio 2012 如何在未安装Office 2010的计算机上安装带有MS Access数据库的Visual Studio 2010安装项目? - How to install visual studio 2010 setup project with MS Access database on a computer which is not having Office 2010 installed? 运行 Visual Studio 安装项目安装的应用程序时出现 Microsoft.Bcl.AsyncInterfaces 错误(在 CSVHelper 方法中) - Microsoft.Bcl.AsyncInterfaces error (in CSVHelper method) when running the application installed by Visual Studio Setup project 使用C#从WPF应用程序修改.mdf文件 - Modify .mdf file from a wpf application using C# 如何在Visual Studio 2010中添加非英文命名文件来设置项目? - How add a non-english named file to setup project in Visual Studio 2010? 使用自包含部署模式为 .NET 创建 Visual Studio 安装项目 5 - Create Visual Studio Setup Project using self-contained deployment mode for .NET 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM