简体   繁体   中英

Getting “Access to database file is not allowed” error when creating a build for 64-bit windows machines

I am creating a new version of an existing Windows desktop application that has been working well for several years. The application uses a SqlServerCe database, which is installed to the DataDirectory of the machine. The new version of the application is being created to be compatible with 64-bit machines. I have migrated the code to a Parallels VM running Windows 10 on my Macbook, and I am building the new version using Visual Studio 2017. The build works fine (I am using a Setup Wizard project to create the Installation files). But, when I install the application, I get an error on the first attempt to access the database.

The error is: Access to the database file is not allowed. [ 1884,File name = C:\\ProgramData\\CompanyName\\ApplicationName\\AppDataBase.sdf,SeCreateFile ].

The .sdf is not marked as read only. If I go to the .sdf file, once it is installed, and give read, write, and modify permissions to Everyone (using the file explorer), then the application can access the file and there is no error. However if I try to do this from within my code using File.SetAccessControl, I get the access error again. I don't see any way to set permissions on the .sdf file during the installation process, using the Setup Wizard functionality (file system view).

Here is the connection string I am using:

<connectionStrings>
<add name="ApplicationName.Properties.Settings.ApplicationNameConnectionString"
            connectionString="Data Source=|DataDirectory|AppDataBase.sdf"
            providerName="Microsoft.SqlServerCe.Client.4.0" />
</connectionStrings>

This worked fine when building using Visual Studio 2010 and deploying on x86 machines. Thank you, in advance, for any advice you can give.

Sounds like it could be UAC

Make sure you're running the exe (not the msi) and that you have Privileged set to true in the LaunchConditions for your installer project

Also make sure you run the application by right clicking and selecting "Run as Administrator"

In essence - your location is wrong. The Folder %SystemDrive%\\ProgramData\\MyCompany\\MyApp for Windows 7 (and higher) is by default read-only for non admin users and should be used for machine-configuration stuff. I'd recommend you reading

https://blogs.msdn.microsoft.com/patricka/2010/03/18/where-should-i-store-my-data-and-configuration-files-if-i-target-multiple-os-versions/

Correct location (I'm assuming basing on fact that it is db file that should be shared across multiple users) is Per Machine “Documents”

“Document” type files that users create/open/close/save in the application that are used across users. These are usually template or public documents.

Example: MyTemplate.dot

Windows 7: C:\\Users\\Public

Vista: %SystemDrive%\\Users\\Public

XP: %ALLUSERSPROFILE%\\Documents

Environment Variable:

Vista/Win7: %PUBLIC% Note: Does not exist on XP

Known Folder ID: FOLDERID_PublicDocuments

System.Environment.SpecialFolder: System.Environment.SpecialFolder.CommonDocuments

CSIDL: CSIDL_COMMON_DOCUMENTS

为什么不简单地使用%APPDATA%?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM