简体   繁体   English

从app.config文件读取值时出现问题

[英]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 我正在用C#制作一个Windows应用程序,在其中我添加了一个文件作为app.config文件。我已经在该文件中写了一些代码作为

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

In 'program.cs' file i am reading this value as 在“ program.cs”文件中,我正在读取此值

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. 在本地计算机上,我可以从配置文件中检索值,但是每当我构建该应用程序并在其他任何计算机上运行时,我都无法从配置文件中读取该值。我正在尝试在Windows 7上运行我的应用程序,请帮助我。预先。

app.config is renamed to <MyProgramName>.exe.config when you build. 构建时, app.config被重命名为<MyProgramName>.exe.config When your program runs it will look for that <MyProgramName>.exe.config file, not app.config . 程序运行时,它将查找该<MyProgramName>.exe.config文件,而不是app.config

You need to deploy the renamed file ( <MyProgramName>.exe.config ) along with your program. 您需要将重命名的文件( <MyProgramName>.exe.config )与程序一起部署。

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). 在您的情况下,您需要复制OBViewer.exeOBViewer.exe.configOBViewer.exe依赖的任何其他文件(例如,debug / release目录中的其他.dll程序集)。

By the way, this renamed file is often commonly referred to as "app.config", even if it doesn't have the same filename. 顺便说一句,即使没有相同的文件名,这个重命名的文件也通常被称为“ app.config”。

and the app.config file exists on the other machine? 并且app.config文件存在于另一台计算机上? Before reading check if it exists 阅读之前检查是否存在

The exception you get says whats incorrect: "FileNotFoundException" 您得到的异常表明什么不正确:“ FileNotFoundException”

EDIT here is the correct way! 在这里编辑是正确的方法!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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