c# - 从同一解决方案 C# 中的另一个项目中读取配置属性
问题描述
我有一个名为WebServiceProject的解决方案。
在这个解决方案中,我有三个项目:
- 通用 (类库)
- 用户界面 (WinForms 项目)
- WebService (WCF 服务项目)
在WebService项目中,我有一个app.config
包含以下内容的文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b72a5b561321d079">
<section name="WebService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b72a5b561321d079" requirePermission="false"/>
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup>
<appSettings>
<add key="ConnectionString" value="mydatabase@localhost"/>
</appSettings>
<system.serviceModel>
<bindings/>
<client/>
</system.serviceModel>
</configuration>
在WebService项目中,我执行一些计划的例程,调用Common项目类读取ConnectionString
源app.config
文件:
if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["ConnectionString"]))
{
// I do something
}
如果我单独启动WebService一切正常。
在WinForms项目UserInterface中,我有一个按钮,可以像WebService一样启动Common项目中的例程。
但是,如果我将UserInterface项目“设置为启动项目”,则前面的代码会引发错误,因为我没有在UserInterface项目中指定。ConfigurationManager.AppSettings["ConnectionString"]
ConnectionString
app.config
所以,我的问题是:如果我将UserInterface项目“设置为启动项目”,如何从WebService项目事件中读取ConnectionString
属性?更一般地说,我如何从与已执行项目不同的另一个项目中读取属性?app.config
解决方案
只需读取文件并将其解析为XML
. 然后检索您的value
阅读兴趣key
。
推荐阅读
- docker - 从 Azure 管道内的 Docker 任务中检索图像摘要值
- django - 从 postgresql 数据库中提取所有字段的数据并在 django 中创建一个包含一些新字段的新模型
- c# - .Net 核心 - 停止服务/用户管理器被处置/保持服务活动
- reactjs - 如何在反应中将多个值传递给我的函数?
- scala - 如何在scala中读取CSV文件
- server - 原生移动应用的 OAuth 2.0 授权码授权 + PKCE
- javascript - 如何在 HTML 和 Javascript 的动态列表中显示格式化的 HTML 文本
- javascript - 如何在打字稿中实现具有多个匿名函数的接口
- javascript - Chart.js 图表不显示(引导程序)
- javascript - jQuery onload 代码如何可重用于缩略图 2 和 3