gpt4 book ai didi

c# - 将多个 app.config 移动到更全局化的地方

转载 作者:太空宇宙 更新时间:2023-11-03 14:02:29 24 4
gpt4 key购买 nike

我们有几个由多个测试人员在多个服务器上运行的 c# selenium 测试套件。

如果可能的话,我想简化设置。目前我们有多个 c# selenium 项目,每个项目都有自己的 app.config。当我想更改服务器时,我需要更改每一个 app.config。我的 app.config 目前看起来像这样:

<connectionStrings>
<add name="Company"
connectionString="Data Source=func3_db1;Initial Catalog=CompanyProduction;Integrated Security=SSPI;"/>
<add name="CompanyProductionEntities"
connectionString="metadata=res://*/DataAccess.CompanyEntities.csdl|res://*/DataAccess.CompanyEntities.ssdl|res://*/DataAccess.CompanyEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=func3_db1;initial catalog=CompanyProduction;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;"
providerName="System.Data.EntityClient" />
</connectionStrings>

我们在 Windows 环境变量中也有一些设置。这是一种晦涩的方法,但效果很好。要访问这些设置,我们只需执行如下操作:

var value = Environment.GetEnvironmentVariable(varName, EnvironmentVariableTarget.User);

因此,我们有一个名为“CompanyTestBrowser”的变量,它可以设置为“Firefox”或“Chrome”或“IE”。

我喜欢环境变量,因为运行我们所有 selenium 测试的 powershell 脚本可以在需要时轻松更改变量。

但是,我似乎无法从这个 app.config 中提取那些数据库字符串。我怎样才能将它们转移到更具全局性和动态性的东西中。理想情况下,我只需要在一个地方设置?理想情况下,我可以像另一个一样将它们移动到环境变量或位于 c# 项目外部的配置文件中。

谢谢!

最佳答案

我的建议是,将连接字符串保存在网络位置 ex 的 xml 文件中。\machine-name\DBConnectionString.config 格式如下

<connectionStrings>
<add name="Company"
connectionString="Data Source=func3_db1;Initial Catalog=CompanyProduction;Integrated Security=SSPI;"/>
<add name="CompanyProductionEntities"
connectionString="metadata=res://*/DataAccess.CompanyEntities.csdl|res://*/DataAccess.CompanyEntities.ssdl|res://*/DataAccess.CompanyEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=func3_db1;initial catalog=CompanyProduction;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;"
providerName="System.Data.EntityClient" />
</connectionStrings>

在您的应用程序启动例程中,读取\machine-name\DBConnectionString.config 文件并使用以下代码片段更新应用程序配置文件,

// Get the application configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);

// Create a connection string element and
// save it to the configuration file.

//Read the \\machine-name\DBConnectionString.config file

// Create a connection string element.
ConnectionStringSettings csSettings =
new ConnectionStringSettings("My Connection",
"LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;" +
"Initial Catalog=aspnetdb", "System.Data.SqlClient");

// Get the connection strings section.
ConnectionStringsSection csSection =
config.ConnectionStrings;

// Add the new element.
csSection.ConnectionStrings.Add(csSettings);

// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);

希望这对您有所帮助。

关于c# - 将多个 app.config 移动到更全局化的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10351193/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com