winforms DataDirectory location - c#

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.

Related

how to publish C# application with the access database

I created a C# application which run with Microsoft Access database and after I deployed the project and installed it on C drive the database file becomes read only, and, if I install it on D or another drive it works fine.
Please if any one can help it is appreciated (SIS is access database file) the problem is i want to make it work in C drive also.
this is my setup SIS is the access file
And this is the connection string im using
String cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\SIS_DB.accdb;";
Your problem is your database file is in %ProgramFiles%. It should be in %AppData%
There are two ways to resolve
1.modify the setup project.
when you make the setup,you should specify the path of f.mdf,ensure that the file will install into AppData folder.
2.copy f.mdf to AppData folder by app.
every time you run you app,the first thing is to copy the file to AppData folder,
you can add the follow code in your Main(or init) method and try again:
string sourcePath=#"C:\PROGRAM FILES\DEFAULT COMPANY NAME\SETUPER2";
string appDataPath= Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string fileName="F.MDF";
System.IO.File.Copy(sourcePath+"\\"+fileName, appDataPath+"\\"+fileName ,false);
*1 is better.
You are old school ... the C drive these days is forbidden area.
Use either the Program Data folder for application specific data - or, for user data, the %AppData% folder where you create a folder for your application and use this folder for the user's data.

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");

Creating a relative path to a Database in Asp.net for a library

In school I am part of a team of four working to create a GUI to translate the paper records of a made-up company and their functionality to a digital format. We're using an ASP.NET website for this purpose.
Basically we use stored procedures and C# classes to represent the database. The folder we're using for the project contains the site and the libraries in separate folders. If I try to open the site from the folder containing both these elements the site will not run. I want to know if there is some way I can set up a relative path to the database in the Settings.Settings.cs file (or by some other means) of my libraries so I don't have to constantly change the database location for the connection string value every time we move the project.
I suppose I should also mention that the database is in an App_Data folder.
You want to use Server.MapPath(...)
The MapPath method maps the specified relative or virtual path to the
corresponding physical directory on the server.
if your database is in App_Data folder, you should use a connection string like this in web.config:
<add name="Database1ConnectionString1"
connectionString="Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|\Database1.mdf;
Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
|DataDirectory| will refer to App_Data, wherever you install your web app.

Store db file in shared directories

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

Create sql server compact file in appdata folder

I am developing a simple piece of software which uses Entity Framework code first and sql server compact 4. At the moment this setup works. Entity framework creates the sql server compact file if it doesn't yet exists. The path to the database is defined from within a connectionstring which is stored inside the app.config file. It is build up like this:
<connectionStrings>
<add name="DataContext"
connectionString="Data source=Database.sdf;"
providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
However, I want to place the database in a folder within the current user's Application Data folder (the C:\Users\User\AppData\Roaming folder on my win7 machine). I've tried setting the Data source of the connectionstring to something like %APPDATA%\Database.sdf, but this doesn't work, I get an "Illegal characters in path" exception.
I want to stick with the connectionstring method, because I'd like to use a different database for my unit tests than with my actual application. This way it is easy to modify the database by placing an app.config file in the root of the project.
Can someone steer me in the right direction?
Use below:
AppDomain.CurrentDomain.SetData("DataDirectory", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));
<connectionStrings>
<add name="DataContext"
connectionString="Data source=|DataDirectory|Database.sdf;"
providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
connectionString="Data source=Database.sdf;"
This tells your app to look for Database.sdf in your app’s current working directory; which could be anywhere and may not be writeable. You need to look at a location you specify:
connectionString="Data source=|DataDirectory|Database.sdf;"
ADO.NET looks for pipe characters in connection strings and expands them to the value of the property of that name in the application’s domain. So what’s the value of the DataDirectory property? It’s supposed to be set by whatever deployed your application:
.MSI installers set it to the app installation folder. If you allow the user to choose the installation folder, they choose the DataDirectory as well. This is why you should always use |DataDirectory| and never a hard-coded path.
ClickOnce defines a special data folder in your project.
Web apps use the App_Data folder.
The Visual Studio debugger uses the debug folder.
Any Visual Studio files in your project with a “copy to output directory” property will be copied to DataDirectory. In most cases DataDirectory will be a read-only folder. This is fine if your data is read-only, but if you want to write to it you will have to copy your data to a writeable location. Probably the best place is Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData)). There are several ways of doing this:
If you are creating an empty new data file, just use your API’s standard CREATE DATABASE or new SqlCeConnection() or whatever.
If you want to start with a pre-populated seed or starter database, include the seed database in your project . In your application startup, check if the database exists in the SpecialFolder.ApplicationData folder and, if not, copy it there.
If you search the web for sample code on creating local database you will run across a lot of bad advice. Do not do the following:
new SqlCeConnection(#"Data source=c:\users\me\myApp\Database.sdf;"); // Do NOT do this!
I hope I don’t have to explain why hard-coding the path to your data is wrong; but be aware that unless you specify a full path in your connection string, the path is relative to your current working directory.
using (var conn = new SqlCeConnection(#"Data source=|DataDirectory|Database.sdf;"))
{
conn.Open();
// No No No! This throws an Access Exception for Standard users,
// and gets deleted when you repair the app!
var cmd = conn.CreateCommand("INSERT INTO Table (column1, column2) VALUES (#p1, #p2)");
...
}
Do not try to modify the data in DataDirectory. Not only is this directory not always modifiable by users, it is owned by the installer not by the user. Repairing or uninstalling the application will delete all the user’s data; users don’t like that. Instead, copy the installed data to a folder writable by the user and make all changes to the copy.
AppDomain.CurrentDomain.SetData("DataDirectory",
// Wrong, this overwrites where the user installed your app!
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));
Do not change the value of DataDirectory in your code, it is set by the installer and if you change it you won’t know where your data was installed. If you are creating an empty database, just open it in your final location. If you are going to make a copy, open the installed database, save it to the user’s location, close the installed database, and open the copy.
I also discourage saving data to Environment.SpecialFolder.CommonApplicationData. This may not be writable by users, and unless there is a very very good reason all users must be allowed to change other users’ data, each user should have their own database.

Categories

Resources