2

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.

rogdawg
  • 687
  • 3
  • 11
  • 33

3 Answers3

2

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"

Jeff
  • 35,755
  • 15
  • 108
  • 220
  • Thank you for your response. I ran the installer as the adminstrator (using Setup.exe, instead of the .msi file) but, I still get the error when I run the application. One of the requirements of the application is that a normal, non-administrator person should be able to run it. Until now, it worked when I put the database in a subdirectory of the ProgramData directory (isn't that what the directory is for?). But, now it doesn't work, and I can't find any way to get it working again. Ugh! – rogdawg Oct 15 '17 at 20:35
  • to clarify: If I run the application as administrator, it works. But, as I said, one of the requirements for the application is that it should run under normal privileges. – rogdawg Oct 16 '17 at 09:58
0

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

Community
  • 1
  • 1
Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89
  • Thank you very much! This is what I was looking for! I removed anything in my code that manipulated the ProgramData location, and I removed the legacy Deployment project as a way to publish the application. Then, I used the Publish functionality of the project (after checking all the settings), and it worked! Thanks very much. I had not seen anything that let me know about the change in folder location so, I would have struggled with that for a long time. – rogdawg Oct 19 '17 at 10:25
0

why not simply use %APPDATA% ?

Felipe Valdes
  • 1,998
  • 15
  • 26