views:

185

answers:

4

My guess is that this question will fall under the category of "duh", but, nevertheless, I'm confused.

When using config files in, for example, a Windows Forms Application, the config file can be found in C:\Program files\CompanyName\ProductName\Application.exe.config. However, with the class library I'm developing I do not see a "ClassLibrary.dll.config" file in the install folder after installing it (in tandem with another project) from Visual Studio. Even though I do not see the file anywhere, retrieving data from it works correctly. Plus, running the following code from a method within the class library returns the path you would expect: C:\Program files\CompanyName\ProductName\ClassLibrary.dll.config.

If someone could shed some light on what I'm missing here, that would be really awesome.

public static string MyMethod()
{
Assembly assem = Assembly.GetExecutingAssembly();
Configuration config = ConfigurationManager.OpenExeConfiguration(assem.Location);

return "The assembly location was: " + assem.Location + Environment.NewLine +
"The config file path was: " + config.FilePath;

// Gives me "C:\Program files\CompanyName\ProductName\ClassLibrary.dll.config"

}
+2  A: 

Depends on the version of .NET. Prior to .NET 4.0, class libraries cannot use their own app.config files unless you do custom stuff to read the data in. In these scenarios, you should have the initialization of your class libraries require all of the appropriate data and pass that in from your consuming class's configuration (i.e. ultimately your winform's app.config file).

Jaxidian
Hmm... That's confusing because I'm using version 3.5 and I added the config file without any problems. Works the way I want and everything. I'm just confused about why the file seems to be invisible.
Nick
I bet you that it is getting its values from your winform's config file and not the config file you think it is. Look closer.
Jaxidian
The value that's being retrieved and displayed to me is only located in the class library's config file. I have not created any other config files in this particular solution.
Nick
@BadNinja: Look in the config file in your `bin` folder, not in the project folder.
Adam Robinson
Its getting the deafult value from your settings file. Set GenerateDefaultValueInCode to false in the property editor of the settings variable - that'll force it to read it from the config file of the consuming app. But, if the value type is string, then absence of the appropriate section in the consuming app's config will not throw an error, but will only set a null value.
Partha Choudhury
@Adam: I can see the config file in the bin folder, but that's not what I'm after. I'm curious where the file will reside after installation on a user's computer.
Nick
@BadNinja: It will reside in the application folder under the `YourExeName.config` name.
Adam Robinson
@Adam: Well, normally it would be YourExeName.exe.config, but my entire point was that no such file existed in my specific scenario. Thanks anyways tho. :-)
Nick
@BadNinja: No such file existed *in the `bin` directory*?
Adam Robinson
It is possible to redirect the config file into a subdirectory. I forget where those options are but they're somewhere within the compile/deploy settings.
Jaxidian
A: 

Your class library will use the config file of the executable that called it.

TLiebe
The executable that called it doesn't have an associated config file. (I created the calling application strictly to test the library.)
Nick
But why is ConfigurationManager lying to him about the name of the config file? Seems like there's some trickery going on under the hood.
Charles
Are you running the program in the Visual Studio debugger? If so, your config file will be something like <appname>.vshost.exe.config.
TLiebe
No, I installed it from visual studio and ran the exe in the install folder afterwards.
Nick
+1  A: 

In a class library, the app.config file is pretty useless. Application setting values get stored in the Settings.settings file and are compiled into the Settings.designer.cs file.

If you modify the value of one of your application settings in app.config directly, that won't have an effect on the setting value seen while running the application. You have to actually open the Settings editor, at which point it will notice the difference between the app.config file and the Settings.settings file and inquire as to whether you'd like it to update your Settings.settings file using values from app.config.

Steven G.
Without going into the nitty-gritty of why I'm doing what I'm doing, suffice it to say for my purposes it works perfectly. :-) I would just like to know where the file will ultimately go after deployment.
Nick
The app.config file of a Class Library doesn't usually go anywhere after deployment. And in your sample code, if you evaluate the Configuration object returned by your call to OpenExeConfiguration, you'll see that the HasFile property is false. That is, it's telling you that there is no such file as "ClassLibrary.dll.config."
Steven G.
Ahhhhh....okay! Thank you very much, Steven! HasFile does indeed return false. :-)
Nick
A: 

Your default value is stored in the dll.

But let's say you change the setting in code and save it. Where does it get stored for next time?

In WinXP look in:

C:\Documents and Settings\username\Local Settings\Application Data\

or in Windows Vista/7 look in:

C:\Users\username\AppData\Local\

You will find a folder named after your application and drilling down in to that folder, you will find a file named user.config.

For example:

C:\Documents and Settings\username\Local Settings\Application Data\MyApp\myapp.exe_urlla1ii3sytrhx0adqtjnjuc24oacqpgu4\1.0.0.0\user.config

JohnForDummies
Okay, that makes sense that the default value would be stored in the dll. But why then is it that my code posted above returns a full path to a dll.config file that does not, apparently, exist?
Nick