Store db file in shared directories - c#

when i am install my application to client machine which have window 7 then give me error that db file don't have read or write permission.
how can i give permission to that file ?
i already read other solution that i should store the db file in shared directories which have read write permission for every user.
but i want to store my database file into my program files folder and want to give permission to that file so it can also work in window 7 .
thanks
EDITED :--
as now all of you suggesting to save the db files in shared folder so my new question is now
how to store .mdf file in shared folder when making setup in c#. what changes should i make in app config file. currently its storing program files in my application folder where my application don't have read write permission .
currently my mdf file is on root and my app config file have this line of code :
<add name="HotelReservationSystem.Properties.Settings.HotelReservationDBConnectionString"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\HotelReservationDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
providerName="System.Data.SqlClient" />

Unfortunately after Windows XP this is NOT possible. In order for a user to write to anything in Program Files they need Administrator access. Not only do they need Administrator access but they also need UAC permission.
So you have to options
Place the database in a read/write folder like ApplicationData
Make your app run as admin UAC rights by adding the setting in app.manifest

Related

Using impersonation to modify the config file on app startup as a non-admin user

When my application starts I run some code to encrypt certain connection strings in my config file(s).
The config files are located in the installation directory in "C:\Program Files (x86)\myapplication".
When the user starting the application is not an admin (which is always the case) the application gives an error because it cannot edit a file in program files if not an admin.
I have some difficulties in trying to impersonate an admin user.
I tried some different approaches I found on the net but with no success.
I hope someone can provide me with a way of achieving this.
What I DON'T want is a prompt for an admin user to login, or change the config file's manifest to require admin privileges to continue.
I DO want to temporarily gain admin access (impersonate admin) to encrypt\modify the config file and then continue as the real user.

Access to database file not allowed (SQL Server Compact) in Visual Studio 2010 C#

First point: I believe this question has already been answered here but it doesn't solve my problem. I'm also not (yet) allowed to comment on the question because of my reputation is less than 50 points.
When my program is installed on a computer that doesn't have admin privileges, it throws this error
Access to database "PayeeList.SDF" is not allowed
when in App Manifest, I add start program as administrator, this problem doesn't come up, but it is a bit annoying when I start my program and it asks for admin permission, I want my program to run without admin permission.
these are my file installation properties and file properties of my database below.
Second point:
Please let me know if I'm doing this correctly.
Solution in the original question on Stackoverflow:
Make sure you use |DataDirectory| in your connection string. Here is
an example
connectionString="Data Source=|DataDirectory|MyDB.sdf"
If I change this will this fix my issue?
Third point: I still get this error when I click OK.
Require any more information, please let me know.
There might be two things to consider :
Installation directory :
Without Admin privilege, Windows will not allow to change any file in installation directory ( Program Files or Program Files (x86)).
You can create your own directory and store your database in :
User's Directory\AppData\Roaming\
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
it is accessible to current logged-in user.
For all users you can use :
C:\ProgramData
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
it is accessible to all users.
ConnectionString :
You can store connection string in Global Static Property Or variable so that it can be easily accessible within current application.
You can use Application.StartupPath to get the current application directory.
Or you can modify you connectionstring in App.config file :
You can also use code to set dataDirectory path :
string path =Application.StartupPath; // or Any Path
AppDomain.CurrentDomain.SetData("DataDirectory", path);

Windows Form Project(C#) is moving file to User's Directory

I have a Windows Form project and created a Setup Wizard Project so I users can install it in Next Next Finish way.
This project uses Microsoft Access as database and I properly added the database file to SetupWizard and everything was ok.
HOWEVER, after reinstallations I notice windows was copying the database file from "Application Folder" to C:\users\myUser\AppData\Local\VirtualStore\Program Files (x86)\.
And just FYI, my connection string in app.config:
<connectionStrings>
<add name="<ProjectName>.Properties.Settings.databaseConnectionString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\database.mdb;Persist Security Info=True;Jet OLEDB:Database Password=MyPassword"
providerName="System.Data.OleDb" />
</connectionStrings>
If I just gather the .exe from ProjectOutput and the database.mdb in the same folder it works perfectly.
When I make the install it copies the database file to C:\users\myUser\AppData\Local\VirtualStore\Program Files (x86)\ folder and its a problem since I want the user to install it where he pleases and that the file stays only there. (It is a software requirement btw).
I've also tried installation to "current user" and "all users" and the behavior is the same.
You have discovered folder redirection on UAC systems, that's what happens when you write to restricted folders on some OS versions:
http://blogs.windows.com/windows/archive/b/developers/archive/2009/08/04/user-account-control-data-redirection.aspx
Your code needs an elevation manifest for it to ask the user to elevate to admin. This might help:
http://www.codeproject.com/Articles/17968/Making-Your-Application-UAC-Aware
and you need requestedexecutionlevel administrator.
If you want limited users to be able to use your app then redesign it so it does not write to things that require admin privilege. That's what folders like User's Application Data are for.

winforms DataDirectory location

I have a c# winforms app that uses SQL compact and here is the connection string for the DB:
<add name="Test.Properties.Settings.TestManagerConnectionString"
connectionString="Data Source=|DataDirectory|\Database\TestManager.sdf"
providerName="Microsoft.SqlServerCe.Client.3.5" />
How can I get the path that the connection string is pointing to? ie: C:\Users\name\AppData... etc
Why do you want to move it to a different directory? By default a non-administrative user does not have access to make changes in the Program Files directory tree. AppData is a user specific store that (in the case of network hosted profiles) points to the appropriate directory on the network for the user's app data directory.
If you want to share data for multiple users, there is the All_Users Data Directory. I'm not sure how to specify it with the SqlCE connection string though.
Update
Here is the answer to determining the appdata directory.
To set the DataDirectory property, call the AppDomain.SetData method.
If you do not set the DataDirectory property, the following default
rules will be applied to access the database folder:
For applications that are put in a folder on the user's computer,
the database folder uses the application folder.
For applications that are running under ClickOnce, the database folder uses the
specific data folder that is created.

Modify ConnectionString in .EXE.CONFIG file with Windows7?

I have an SQL Server CE 4.0 (SDF) file that under the Users Application Data Folder (because due to Win7 UAC you cannot modify the DB if it is in the \Programs Files\ folder. But the problem remains that I need to now (on install) modifiy the .EXE.CONFIG file (ConnectionString) to point to the new path for the DB, however obviously I do not have access to modify the .EXE.CONFIG file either in the \Program Files\ folder ...
So how do people resolve this issue?
Can you deploy the .EXE.CONFIG file to the users Application Data Folder as well with the SDF file? If not how? I cannot beleive you need to grant special access rights to the \Program Files\Application folder to be able to have full access...
I am using VS2010 and using a standard Deployment Project (MSI) and I don't see anyway to deploy the .EXE in one place the the .EXE.CONFIG in another (can this be done? Is this the right solution?)
Thanks,
Use the %APPDATA% variable in the connection string and you won't have to modify the configuration file. This is also more flexible than hard-coding a path that contains a username, since it will allow all users of the computer to use your program and have their own .sdf files.
However, the environment variables are not automatically expanded in .NET, so you need to enforce it by using the Environment.ExpandEnvironmentVariable method:
// Example appSetting element:
// <add key="examplePath" value="%APPDATA%\example.txt" />
string path = System.Configuration.ConfigurationManager.AppSettings["examplePath"].ToString();
path = Environment.ExpandEnvironmentVariables(path);
System.IO.File.WriteAllText(path, "foobar");

Categories

Resources