views:

1174

answers:

4

I'm writing a desktop app which needs a simple persistence layer - I found out about SubSonic and it's capability to work with SQLite. However I need to keep the database file in user's AppData folder and don't know how to put such value into app.config - I don't want to use absolute paths.

Can app.config somehow access enviroment variables or reference application data folder?

+1  A: 

There's no way to specify the AppData folder in the app.config for a connections string.

But what you could do is write the value to the config file either during install or when the application is first run.

Adam
Could you write/link an example on how to do that?
dahpgjgamgan
You can read/write the config file using the ConfigurationManager Class.Example: http://social.msdn.microsoft.com/forums/en-US/winforms/thread/f26d0863-06be-45c1-b42d-e0816dc9eb58/MSDN docs: http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx
Adam
A: 

The "framework way" of finding appdata is to use Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

This will find the correct appdata path regardless of platform.

blue_fenix
A: 

There are several ways if you are using ASP.NET , in either language

Server.MapPath("~") will return the root of the application as a full path name then you can just add "/app_data" to it to get you're full path.

Alternatively inspect the HttpContext.Current.Request and HttpContext.Current.Application there are numerous ( and much better then the one I just mentioned ) properties that will provide you with the same folder - being the root of the application as s full path.

Note that these should all work even if you have the application as a virtual folder and a regular folder with an application configures in IIS on that folder

However this is only possible at runtime , so it can't really be mentioned in the app.config. you could try using relaltive paths from where the app.config is resident IE "../App_Data" or "/App_data" but I'm not sure of you're exact requirements.

Good luck

+2  A: 

For subsonic v2.x I would ignore the app.config connection string and just set it at runtime before working with the database. The provider name stays the same of course.

string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"MyApplication\Northwind.db3");

DataService.Providers["Northwind"].DefaultConnectionString =
            String.Format(@"Data Source={0};Version=3;New=False;Connection Timeout=3", dbPath);
P a u l
See also http://stackoverflow.com/questions/2079781/how-can-i-tell-subsonic-2-to-use-a-different-config-file
Rory