Sqlite database cannot be accessed after installation in WPF - c#

I have created a WPF application and I have used a visual studio setup project inside the original solution to create an installer. This installer is created in a correct way and install the application on the system.
The problem is the fact that inside the application a sqlite database need to be created. Until the application is inside visual studio no problem are present, but once installed inside the main window an exception is raised:
System.UnauthorizedAccessException: Access to path 'C:\Program Files (x86)\User\TestApp\database.sqlite' denied
The code I use inside the app is the following:
if (!File.Exists("database.sqlite"))
{
SQLiteConnection.CreateFile("database.sqlite");
SQLiteConnection sQLiteConnection = new SQLiteConnection("Data Source=database.sqlite;Version=3;");
sQliteConnection.setPassword("1234");
sQliteConnection.Open();
}
Can you tell where the problem is? I need to place those elements in a special folder that is accessible after the installation? Or there is another method to ensure the app works after the installation on target machine?

Files in the Program Files or Program Files (x86), or any folder underneath, are typically set for read-only access unless your program is run elevated. This requires any write-access files or databases to be located elsewhere. Depending on what you're putting into it, ApplicationData or LocalApplicationData (from Environment.GetFolderPath) are the typical locations.

Related

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
why not simply use %APPDATA% ?

Ms access and vb.net [duplicate]

I have deployed my application to be ready for use by another user (another computer), but when I try to add the data to the database I get the error
Operation must use an updatable query
The error is like this (This is when I already deployed my program and run under application, not under Visual Studio):
But it works perfectly under visual studio, the image like this (note that, the error on the image above appear once I click the submit button, it supposed to stored in the database, and display it on the datagridview as like image below):
And also I got another problem, the delete function is not running, the error on the add and delete appear once I deployed my program, but I will post that on another thread.
How do I solve this?
When a Visual Studio application is under development it resides in a folder to which the developer has read/write access. This is obviously necessary since the developer needs to be able to edit the source code files. If you place a database file "in with the code" then the application can update the database file because it is in a "writable" location.
However, if on deployment the database file stays "with the code" and the installer puts the files (i.e., the executable file and the database file) into %ProgramFiles% on the target machine (for example, C:\Program Files\MyApplication) then the average user will not have write access to that location. Files in %ProgramFiles% are normally restricted to read-only to protect the system from malware.
Some people will try and configure the installer to grant write access to normal users for some file(s) or folder(s) under %ProgramFiles% but that is a Bad Idea™. The installer should really copy the database file to a location that is normally read/write for the intended user(s): either %USERPROFILE% (for a specific user), or %PUBLIC% (for all users).

How to set the current\working directory of a WPF application to the installation path

In my C# WPF application I need to access some configuration files via a 3rd party library. This library requires to have the configuration file located in the same folder as the executable of my application. So i have no chance to change this behaviour.
While running my application in Visual Studio 2013 it works fine. I can access the configuration file since I have just copied it to the relevant folder.
But if I install my application an run it it cant locate my configuration files because it tries to find it under: Windows\system32.
No my approach is to make it happen that my application looks for the configuration file in the applications install folder.
How can I do that? How can I set the current\working directory of my application to a specific (installation) path in Visual Studio 2013?
try this
string path = AppDomain.CurrentDomain.BaseDirectory.ToString();
This will give path like "c:\\program...\\installdir\\" where your .exe is located.

My application cannot update Access database after deployment to "C:\Program Files\..."

I have deployed my application to be ready for use by another user (another computer), but when I try to add the data to the database I get the error
Operation must use an updatable query
The error is like this (This is when I already deployed my program and run under application, not under Visual Studio):
But it works perfectly under visual studio, the image like this (note that, the error on the image above appear once I click the submit button, it supposed to stored in the database, and display it on the datagridview as like image below):
And also I got another problem, the delete function is not running, the error on the add and delete appear once I deployed my program, but I will post that on another thread.
How do I solve this?
When a Visual Studio application is under development it resides in a folder to which the developer has read/write access. This is obviously necessary since the developer needs to be able to edit the source code files. If you place a database file "in with the code" then the application can update the database file because it is in a "writable" location.
However, if on deployment the database file stays "with the code" and the installer puts the files (i.e., the executable file and the database file) into %ProgramFiles% on the target machine (for example, C:\Program Files\MyApplication) then the average user will not have write access to that location. Files in %ProgramFiles% are normally restricted to read-only to protect the system from malware.
Some people will try and configure the installer to grant write access to normal users for some file(s) or folder(s) under %ProgramFiles% but that is a Bad Idea™. The installer should really copy the database file to a location that is normally read/write for the intended user(s): either %USERPROFILE% (for a specific user), or %PUBLIC% (for all users).

.NET including class library database (and other resources) with windows app deployment

I have a class library that attaches itself a tiny SQL Server database that resides in its Data Directory. When i'm using this class library with another windows application i see that once i compile my code, the database files get copied to the bin folder of my windows app project. However when i publish the windows app,install, and run it, i get the error 'An attempt to attach an auto-named database for file C:\Documents and Settings\User\Local Settings\Apps\2.0\Data..\DB.mdf failed.' Obviously this folder doesn't have the mdf files.
I guess this won't be a problem if i just add the database files to my windows application project. But surely there's a better way?
You could include an SQL script for creating the database into your "installation/run first time routine".
I guess that you've already stated that having a form of SQL Server is an installtion prerequisite.
For the data files I would recommend that you use a variable connection string for accessing your database. That way you can change the installation routine to include asking the user where they wish to have the data files installed and save that as part of your connection string to the app.config file.
Conversely you could also use the users selection of where to install the app to override the relative path stored for the database within your code (using the same connection string variable as mentioned above).

Categories