对于Visual Studio 2010基于Web的应用程序,我们具有Config Transformation功能,通过该功能,我们可以为不同的环境维护多个配置文件.但是,对于Windows Services/WinForms或控制台应用程序,App.Config文件无法使用相同的功能.
这里提供了一种解决方法:将XDT魔术应用于App.Config.
然而,它并不简单,需要许多步骤.有没有更简单的方法来实现相同的app.config文件?
app-config .net-4.0 visual-studio web-config-transform slowcheetah
var connection = ConnectionFactory.GetConnection(
ConfigurationManager.ConnectionStrings["Test"]
.ConnectionString, DataBaseProvider);
Run Code Online (Sandbox Code Playgroud)
这是我的App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Test" connectionString="Data Source=.;Initial Catalog=OmidPayamak;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
但是当我的项目运行时,这是我的错误:
你调用的对象是空的.
在将使用SlimDX并因此具有非托管代码的项目转换为.NET 4.0时,我遇到了以下错误:
混合模式程序集是针对运行时的版本"v2.0.50727"构建的,如果没有其他配置信息,则无法在4.0运行时加载.
谷歌搜索给了我解决方案,这是将其添加到应用程序配置:
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我的问题是,做了useLegacyV2RuntimeActivationPolicy什么?我找不到任何关于它的文档.
我试图将app.config文件添加到我的DLL,但所有尝试都失败了.
根据MusicGenesis在" 将配置信息放入DLL中 "这应该不是问题.显然我做错了什么......
以下代码应该从我的DLL返回我的ConnectionString:
return ConfigurationManager.AppSettings["ConnectionString"];
Run Code Online (Sandbox Code Playgroud)
但是,当我将app.config文件复制到我的控制台应用程序时,它工作正常.
有任何想法吗?
我正在尝试完成此异常处理程序:
if (ConfigurationManager.ConnectionStrings["ConnectionString"]==null)
{
string pathOfActiveConfigFile = ...?
throw new ConfigurationErrorsException(
"You either forgot to set the connection string, or " +
"you're using a unit test framework that looks for "+
"the config file in strange places, update this file : "
+ pathOfActiveConfigFile);
}
Run Code Online (Sandbox Code Playgroud)
当我使用nUnit时,这个问题似乎只发生在我身上.
我在C#.NET中完成了一个项目,其中我的数据库文件是一个Excel工作簿.由于连接字符串的位置在我的编码中是硬编码的,所以在我的系统中安装它没有问题,但是对于其他系统来说.
有没有办法在应用程序设置完成后提示用户设置路径一次?
我得到的答案是"使用App.Config"...任何人都可以告诉这个App.config是什么以及如何在我的上下文中使用它?
在开发.NET Windows窗体应用程序时,我们可以在这些App.config标记之间进行选择以存储配置值.哪一个更好?
<configuration>
<!-- Choice 1 -->
<appSettings>
<add key="RequestTimeoutInMilliseconds" value="10000"/>
</appSettings>
<!-- Choice 2 -->
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5612342342" >
<section name="Project1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5612342342" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<Project1.Properties.Settings>
<setting name="TABLEA" serializeAs="String">
<value>TABLEA</value>
</setting>
</Project1.Properties.Settings>
</applicationSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud) 我正在尝试在项目中实现自定义配置部分,并且我一直在遇到我不理解的异常.我希望有人能填补这里的空白.
我App.config看起来像这样:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="ServicesSection" type="RT.Core.Config.ServicesConfigurationSectionHandler, RT.Core"/>
</configSections>
<ServicesSection type="RT.Core.Config.ServicesSection, RT.Core">
<Services>
<AddService Port="6996" ReportType="File" />
<AddService Port="7001" ReportType="Other" />
</Services>
</ServicesSection>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我有一个ServiceConfig像这样定义的元素:
public class ServiceConfig : ConfigurationElement
{
public ServiceConfig() {}
public ServiceConfig(int port, string reportType)
{
Port = port;
ReportType = reportType;
}
[ConfigurationProperty("Port", DefaultValue = 0, IsRequired = true, IsKey = true)]
public int Port
{
get { return (int) this["Port"]; }
set { this["Port"] = value; …Run Code Online (Sandbox Code Playgroud) 我正在测试使用app.config文件加载配置属性的.NET应用程序(.exe).单元测试应用程序本身没有app.config文件.
当我尝试对使用任何配置属性的方法进行单元测试时,它们返回null.我假设这是因为单元测试应用程序不会加载到目标应用程序的app.config中.
有没有办法覆盖这个或我是否必须编写脚本将目标app.config的内容复制到本地app.config?
这篇帖子有点问这个问题,但作者真的从一个不同的角度来看待它.
编辑:我应该提一下,我正在使用VS08 Team System进行单元测试.
是否有相当于app.config库(DLL)?如果不是,存储特定于库的配置设置的最简单方法是什么?请考虑该库可能在不同的应用程序中使用.
app-config ×10
c# ×7
.net ×6
.net-4.0 ×2
ado.net ×1
dll ×1
mixed-mode ×1
nunit ×1
settings ×1
slowcheetah ×1
unit-testing ×1
web-config ×1