Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,

I have an project that has an associated app.config file.
In this file I have a couple of keys that I want to use to customize where certain things live on different computers, as follows:

<?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
  </startup>
  <appSettings>
    <add key="DBFileLocation" value="C:\data.db3"></add>
    <add key="DBConnString" value="Data Source=C:\data.db3;Pooling=true;FailIfMissing=false"></add>
    <add key="DBBackupLocation" value="C:\backup\data.db3"></add>
    <add key="BirthdayCardLocation" value="C:\Program Files\Release\happy_birthday.jpg"></add>
  </appSettings>
</configuration>


I build my solution in release mode, and put it on a computer other than my dev computer.
I then change the strings in app.config to suit the new computer, but the program still refers to the old strings that were in there at compile time.

Why is that?
Posted
Updated 22-Jun-11 21:46pm
v2
Comments
Prerak Patel 23-Jun-11 0:43am    
How do you read the settings?!
Member 7737012 23-Jun-11 0:49am    
I pass in the key as a string:

public string GetAppSettings(string key)
{
try
{
AppSettingsReader reader = new AppSettingsReader();
NameValueCollection appSettings = ConfigurationManager.AppSettings;
return appSettings[key].ToString();
}
catch (ConfigurationErrorsException e)
{
MessageBox.Show("Cannot load key from config file: " + e);
return "";
}
}
Timberbird 23-Jun-11 3:21am    
AppSettingsReader reader is not used anywhere in your code, right?
[no name] 23-Jun-11 2:05am    
hoping you have not hardcoded on page somewhere.
Member 7737012 23-Jun-11 2:32am    
nope definitely not. I know because when I changed the values in app.config, (and thats all I changed) then recompiled and put on other computer, the changes were there. Got me stumped...

Just a quick question. Have you copied the file App.config onto the other machine? If you have, this is your problem - that's not the config file the application picks up. By default, executables pick up config files that have the fullname of the exe, followed by .config so, for instance, if your exe was called MyProgram.exe, the config file would be called MyProgram.exe.config (and not app.config).

App.config is a convenience file that gets renamed for you when it's put into the output folder to match the correct filename. You need to make sure you've done the same on the other machine.
 
Share this answer
 
Comments
fjdiewornncalwe 23-Jun-11 9:48am    
We have a winner... +5.
Pete O'Hanlon 23-Jun-11 10:07am    
Why thank you.
Member 7737012 23-Jun-11 18:40pm    
Yes this is it! Thank you. Man that was driving me crazy!
Member 7737012 23-Jun-11 18:43pm    
A quick question then. How would you use the app.config file? What is its purpose? I guess if you wanted to use it you would have to change the default behaviour of the exe to point to the app.config file...
[no name] 24-Jun-11 5:40am    
Good Call. My 5 too.
I don't think it is accessing the file you think it is.
Check your App.config file on the new machine: Look in the folder where you installed the App. Examine the modification date: when was it changed? Run your program - what values is it using? Check the modification date again. Rename the App.Config file. Run your app. Do you get your exception? I don't think you will...
 
Share this answer
 
AFAIK the config file gets copied to some user-dependent folder. Its position is something like %SYSTEMROOT%\Documents and Settings\[Username]\Local Settings\Application Settings\[Application Manufacturer]\[Application Name]\user.config.

There will be a seperate copy for every user that startet the application. Changes to the original .config file have no effect after first application start except the per-user .config file is deleted as well, or edited directly.
 
Share this answer
 
When your setup will successfully installed on your machine then a file "Myapplication.exe.config" file will also installed. open the file in notepad and changed the connection string. it will refer to updated connection string.
 
Share this answer
 
When the application is compiled and deployed the app.config content will be coped to the file name called [projectname].exe.config file. So change key and value in the [projectname].exe.config file. I sure it will work.

BTW: To read the configuation from app config

use

ConfigurationManager.AppSettings[Key];

This is very simpler that you writing one method called GetAppSettings.
 
Share this answer
 
v2
Take App.Config file with installed file and then read your App.Config, Change Your setting!
 
Share this answer
 
When you would change the connection string in app.config file you have to compile the project every time otherwise it will refer to the old string , i have also faced the same problem and solve it in another way.

Create .ini file and write the connection string in the .ini file and read the connection string from .ini file, that will always refer to the updated one.
Sure this will solve your problem.
 
Share this answer
 
v2
Comments
fjdiewornncalwe 23-Jun-11 9:47am    
This approach defeats the entire concept of the app.config
Change your setting.settings file
 
Share this answer
 
Comments
fjdiewornncalwe 23-Jun-11 9:48am    
This would require a recompile.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900