如何从“WPF”读取值类库中的项目 App.config 文件
我有一个 WpfApplication 项目(Visual Studio 2008),其中包含通过在 Settings.settings 文件中输入“名称”和“值”对生成的 app.config(因此生成“applicationSettings”元素而不是“appSettings”元素)。
我在同一个 Visual Studio 解决方案中添加了一个 class1 类库项目。
** 我已使用“添加现有项目,然后添加为链接”将 WpfApplication app.config 文件添加到 class1 项目中。** 因为发现这是不必要的(即字符串 a = ConfigurationManager.AppSettings.Get("key1") ; 下面的工作没有这个)
我想从 class1 读取 app.config 中的值并探索了以下内容:
string s1 = Settings.Default.appsetting1;
但我不想引用 1 类项目中的 WpfApplication10 项目,因此无法获取引用Settings类。所以这个语法只适用于WPF项目。
string a = ConfigurationManager.AppSettings.Get("key1").ToString();
这确实有效,但前提是我将以下内容添加到 app.config:
<appSettings>
<add key="key1" value="1"/>
</appSettings>
是否有其他方法可以实现我想要的效果,这可能会更好(例如提供类型安全或能够读取 Settings 类属性?
I have a WpfApplication project (Visual Studio 2008) with an app.config generated from entering 'Name' and 'Value' pairs in the Settings.settings file (and therefore generating 'applicationSettings' elements rather than 'appSettings' elements).
I have added a class1 class library project in the same Visual Studio solution.
** I have added the WpfApplication app.config file to the class1 project using "add existing item and then add as link".** since found out this is unneccassary (i.e. string a = ConfigurationManager.AppSettings.Get("key1"); below works without this)
I want to read the values within app.config from class1 and have explored the following:
string s1 = Settings.Default.appsetting1;
But I do not want to reference the WpfApplication10 project from Class 1 project and so cannot get a reference to the Settings class. So this syntax only works within the WPF project.
string a = ConfigurationManager.AppSettings.Get("key1").ToString();
This does work, but only if I add the following to app.config:
<appSettings>
<add key="key1" value="1"/>
</appSettings>
Are there other ways to achieve what I want which may be better (e.g. offering type safety or being able to read the Settings class properties?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以执行以下操作:
如果您需要从 key="key1" 获取值,只需编写此内容(假设您的 app.config 位于正确的目录中)。
You can do the following thing:
if you need to get value from key="key1" just write this(supposing your app.config is in the correct directory).