从app.config获取设置 [英] Getting settings from app.config

查看:83
本文介绍了从app.config获取设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有引用项目B的项目A.如果项目A调用项目B,项目B调用ConfigurationManager.GetSection().它将从项目A的App.Config中获取设置.有什么方法可以从项目B的app.config中获取设置.我希望措辞不要太混乱.我是C#和编程的新手.我试图用谷歌搜索,但不确定如何定义问题.如果需要的话,我会澄清.谢谢.

I have project A referencing project B. If project A calls project B and project B calls ConfigurationManager.GetSection(). It''ll get the settings from project A''s App.Config. Is there any way to get the settings from project B''s app.config. I hope the wording''s not too confusing. I''m new to C# and programming. I tried to google but not sure how to define the problem. I''ll clarify things if need be. Thank you.

推荐答案

首先,您引用的不是项目,而是程序集.在Visual Studio级别上,您可以引用项目,这是一种非常推荐的方法,但这只是一种基于程序集的主要可执行文件模块在程序集之间自动进行引用的方法. />
只需使用System.Configuration.ConfigurationManager.OpenExeConfiguration打开备用可执行文件的配置,请参阅 http://msdn.microsoft.com/en -us/library/ms224437.aspx [ ^ ].

但是,请格外小心.配置管理器会根据可执行文件名自动构建配置文件的路径名.通常,此文件的扩展名是".EXE",但您可以使用".DLL".在.NET中,这些可执行文件之间没有本质区别.您确实可以将* .EXE引用为类库.我认为您不会遇到问题,但请仔细检查配置文件名.

现在,我上面引用的MSDN页面上的代码示例在计算可执行文件的路径名方面非常糟糕.如果您想以良好且高度兼容的方式执行此操作,则可以执行以下操作.切勿使用任何立即常量,尤其是字符串,如此代码所示.选择在引用程序集中声明的某种类型.假设其名称为MyType,则引用程序集的主要可执行模块的名称将为:

Firs of all, you reference not a project, but an assembly. At the level of Visual Studio, you can reference projects, and this is a very recommended way to do, but it is just a way to automate references between of assemblies based on their main executable modules.

Just open the configuration of alternative executable using System.Configuration.ConfigurationManager.OpenExeConfiguration, see http://msdn.microsoft.com/en-us/library/ms224437.aspx[^].

However, be extra careful. The Configuration Manager automatically build a path name of configuration file based on executable file name. Usually, the extension of this file is ".EXE", but you might use ".DLL". In .NET, there is no essential difference between those executables; you really can reference *.EXE as a class library. I don''t think you will face a problem, but double check the configuration file name.

Now, the code sample on MSDN page I referenced above is quite bad in the part of calculation of the path name of the executable file. Here is what to do, if you want to do it in nice and highly compatible way. Never use any immediate constants, especially strings, like this code shows. Pick some type declared in referenced assembly. Assuming its name is MyType, the name of the main executable module of the referenced assembly will be:

string exeName = typeof(MyType).Assembly.Location;



使用此可执行文件的名称传递给对System.Configuration.ConfigurationManager.OpenExeConfiguration的调用.如果使用某些其他应用程序主机,则其他所有获取可执行文件名称的方法都可能会失败,例如,Visual Studio或服务" Windows服务.

—SA



Use this executable name to pass to the call to System.Configuration.ConfigurationManager.OpenExeConfiguration. All other ways of getting executable name may fail if some different application host is used, for example, Visual Studio or ''Services'' Windows service.

—SA


这篇关于从app.config获取设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆