简体   繁体   中英

Problems while reading value from app.config file

I am making one windows application in c# in where i added one file as app.config file.I have written some code in that file as

    <appSettings>
       <add key ="FlagForArchiving" value="true"/>
    </appSettings>

In 'program.cs' file i am reading this value as

ConfigurationSettings.AppSettings["FlagForArchiving"].ToString();

On local machine i can retrieve value from config file but whenever i am building that application and running on any other machine then I cant read the value from config file.I am trying to run my application on windows 7.Please help me.Thanks in advance.

app.config is renamed to <MyProgramName>.exe.config when you build. When your program runs it will look for that <MyProgramName>.exe.config file, not app.config .

You need to deploy the renamed file ( <MyProgramName>.exe.config ) along with your program.

In your case, you need to copy over OBViewer.exe , OBViewer.exe.config , and any other files that OBViewer.exe depends on (eg other .dll assemblies in your debug/release directory).

By the way, this renamed file is often commonly referred to as "app.config", even if it doesn't have the same filename.

and the app.config file exists on the other machine? Before reading check if it exists

The exception you get says whats incorrect: "FileNotFoundException"

EDIT here is the correct way!

if (File.Exists(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile))
{
    MessageBox.Show(ConfigurationSettings.AppSettings["FlagForArchiving"].ToString());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM