User specific app.config - are they possible?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I have VB.Net/SQL Server app that connects to a common instance of SQL
Server but the "initial catalog" entry in the ..exe.config needs to be user
specific. Is this possible, i.e. can I have multiple copies of the
...exe.config and place them directories that will searched when the app
starts depending upon who logged in?

If that can't be don, is it possible to change ...exe.config values after
the application has started?

TIA
 
If that can't be don, is it possible to change ...exe.config values after
the application has started?

Not out of the box, no. MS designed the config system to be an
app-wide, read-only configuration setting system.

You still have options - you could

a) use the registry for user-specific settings
b) use any arbitrary settings class / object for your user settings
and store them in a suitable place
c) use an extension of the .NET config system, to keep the same syntax
and structure, and store those user-specific .config files in a
suitable place. You'll find a few of those on e.g. www.codeproject.com

What a suitable place is, depends on your app - if you have a
desktop-only app, you could likely store them in your user's
"Documents and Settings" folder.

If you need to support roaming users, or if you're dealing with
ASP.NET apps, then suitable will probably rather be the "Isolated
STorage" (read up on it in the MSDN docs!).

HTH
Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
Hi Marc,

Thanks for the very prompt response.

Option c) is the way I would like to go and I did try it before I posted the
question. What I did was deleted my ..exe.config file from my deployment
directory and put a copy in (various) directories under C:\Documents and
Settings\UserName but, each time the app started, it failed to see its
...exe.config file.

What do I need to do for the app to find an appropriate ..exe.config under
C:\Documents and Settings\UserName when it starts?
 
Option c) is the way I would like to go and I did try it before I posted the
question. What I did was deleted my ..exe.config file from my deployment
directory and put a copy in (various) directories under C:\Documents and
Settings\UserName but, each time the app started, it failed to see its
..exe.config file.
What do I need to do for the app to find an appropriate ..exe.config under
C:\Documents and Settings\UserName when it starts?

It won't - the standard .NET config system will *ONLY* look in hte
directory where the EXE resides - and you can't change that,
unfortunately. That's why I was saying you'd need to use an extended /
enhanced third-party version of the .NET config system - the standard
system can't be tweaked to your needs, unfortunately.

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
Hi Peter,
Hi,
I have VB.Net/SQL Server app that connects to a common instance of SQL
Server but the "initial catalog" entry in the ..exe.config needs to be user
specific. Is this possible, i.e. can I have multiple copies of the
..exe.config and place them directories that will searched when the app
starts depending upon who logged in?

If that can't be don, is it possible to change ...exe.config values after
the application has started?

It can be done using another application domain:

- create and configure an AppDomainSetup object
- create a new AppDomain using that AppDomainSetup object above

bye
Rob
 
Hi Marc,

Thanks for the help but my problem is a little more complex than I
explained. Roberts suggestion may be ok but is beyond my skill set I'm afraid.

My problem is that I'm not in control of all the code in this project - much
of it is in DLL's that use the key value of "DataSource" in appSettings to
retrieve the connection string to use. The key value is extracted using
ConfigurationSettings.AppSettings("DataSource") so it is only able to read
the standard appSettings as loaded when the program starts.

Am I correct in my understanding - in a Windows Forms application the
appSetting values are read and cached when the exe starts and there is no way
of changing the cached values and, furthermore, it is impossible to point to
a .config file other than the one in the directory in which the exe file is
located. If this is the case then I guess I'm dead in the water.

This does seem to be very odd. I would have thought the passing in user
specific configuration information when a program starts is a pretty basic
requirement. I'm not asking for "Config Files 101" lesson but is there a
mechanism, within a config file, for it to make decisions of what is should
do, eg if variable X = "A" Then "DataSource" = "ssss" Else "DataSource" =
"AAAA"

Cheers, Peter
 
A simple (?) way to make it work might be have a secondary exe that gets
copied to the current users "documents and settings" directory when the main
exe starts up and the secondary exe can load up the config from there and
pass it back to the main exe as something... :)

Or, You can write your own configuration reader. Its just XML anyway.
 
Hi Girish,

Thanks for the comment. There are a number of fine examples for reading and
writing the ...exe.config file. My problem is that it is too late by then.
The app has started and (as I understand it) the ..exe.config values are
cached and there is no way to change them.

As I see it my only hope appears to be putting sometype of decision making
into the xml itself. Robert's suggestion seems very sound but it's out of my
league. I really am very surprised by this problem - I had just assumed that
passing user specific configuration details to a starting exe would have been
a piece of cake, e.g. processing the .exe.config file out of a directory in
the user's Documents and Settings area.

Cheers, Peter
 
BTW: AppDomainSetup overview already has a simple sample code which shows
the way to do it.
 
Hi Girish,

Thanks again for your interest. I've had a good look at the help files and
the FAQ on www.gotdotnet and I still think this is a bit heavy duty for my
level of knowledge however I'm certainly willing to give it go if, in the
end, I can achieve my objective. I have created a new AppDomain and set the
config file details, eg Newdomaininfo.ConfigurationFile =
System.Environment.CurrentDirectory + "\Newconfig" however,
ConfigurationSettings.AppSettings("DataSource") still returns the value from
the ...exe.config in place when the program started.

No doubt I'm doing something wrong or incomplete but, if I can get this
working, will I be able to change my environment so that the statement:
ConfigurationSettings.AppSettings("DataSource") will, indeed, return a value
from a config file that I select at runtime? I don't want to spend a lot of
time trying to achieve the impossible - the bottom line for me is that:
ConfigurationSettings.AppSettings("DataSource") is *only* way my app can
discover its database and initial catalog. Can I achieve this outcome using
AppDomain?

Cheers, Peter
 
You might want to use a combination of AppDomain + a second boot strapper
Appdomain. i.e. A boot strapper might do the AppdomainSetup call to make the
config to point at the Document & settings for a given user and then create
the AppDomain for your "real" application.

After that your application is supposed to use that particular config file
instead of the exe.config file.

This should work. If its going to be that user dependent, you might be able
to use Registry with HKCU as well.
 
Hi Peter,
Am I correct in my understanding - in a Windows Forms application the
appSetting values are read and cached when the exe starts and there is no way
of changing the cached values and, furthermore, it is impossible to point to
a .config file other than the one in the directory in which the exe file is
located.

Yes, you've summed it up rather nicely. That's the way the standard
..NET config system works - it has very strict naming rules (only
myapp.exe.config, and only in the app's directory), and no refreshing
of the .NET config file when it happens to have changed in the meain
time.
This does seem to be very odd. I would have thought the passing in user
specific configuration information when a program starts is a pretty basic
requirement.

Yes - but MS just basically chose not to mandate what to use for that
case. The .NET .config files are for APP CONFIGURATION ONLY - not
user specific settings. You gotta roll your own for that.

Marc

================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
Hi Marc/Girish/Robert

Thanks again for your input. In the end I did make it work (very well) using
AppDomain.

FYI this is what I did:

The deskstop shortcuts to the app now read:

MyEXEFileName MyDatabaseName MyInitialCatalogName

In the init code for the app I call:

SetDataSource()

which reads:

Private Sub SetDataSource

Dim strCmdLineArg As String() = Environment.GetCommandLineArgs()

' Command line elements 0 = exe name, 1 = database name, 2 = initial
catalog
' Only process is program started with specific database and catalog
names
If strCmdLineArg.Length() = 3 Then
Dim currentDomain As AppDomain = AppDomain.CurrentDomain

Dim strKey As String = "DataSource"
Dim strKeyValue As String = "integrated security=SSPI;data
source="

strKeyValue = strKeyValue & strCmdLineArg(1)
strKeyValue = strKeyValue & ";persist security
info=False;initial catalog="
strKeyValue = strKeyValue & strCmdLineArg(2)
currentDomain.SetData(strKey, strKeyValue)
End If
End Sub

Cheers and thanks - Peter
 
Back
Top