在VS2005 C#项目中,我添加了对System.configuration的引用.在对象浏览器中,我可以看到System.Configuration.ConfigurationManager.在Intellisense System.Configuration中只有旧的ConfigurationSettings,而不是ConfigurationManager.
我的代码System.Configuration.ConfigurationManager.AppSettings ["MySetting"]
突出显示为语法错误,不编译.
在一个不同的项目中,完全相同的设置工作正常...任何线索关于发生了什么?
我们知道IIS缓存ConfigurationManager.AppSettings,因此它只读取磁盘一次,直到更改了web.config.这样做是出于性能目的.
有人在:
http://forums.asp.net/p/1080926/1598469.aspx#1598469
表示.NET Framework不会对app.config执行相同操作,但它会从磁盘读取每个请求.但我觉得很难相信,因为它会变慢.请告诉我他错了或者我必须修复我写的每个控制台/ Windows窗体/ Windows服务.
更新我很遗憾我误解了人们在上面的链接论坛中所说的内容.
我已将多个app.config(每个都有一个不同的名称)文件添加到项目中,并将它们设置为复制到每个构建的输出目录.
我尝试使用以下方法访问每个文件的内容:
System.Configuration.Configuration o = ConfigurationManager.OpenExeConfiguration(@"app1.config");
Run Code Online (Sandbox Code Playgroud)
代码运行,但o.HasFile结束False,o.FilePath结束"app1.config.config".如果我改为代码:
System.Configuration.Configuration o = ConfigurationManager.OpenExeConfiguration(@"app1");
Run Code Online (Sandbox Code Playgroud)
然后代码炸弹"加载配置文件时发生错误:参数'exePath'无效.参数名称:exePath".
如果我复制/粘贴配置文件(所以我最终使用app1.config和app1.config.config)然后代码运行正常,但是,我认为这不是一个好的解决方案.我的问题是:如何使用ConfigurationManager.OpenExeConfiguration正确加载配置文件?
当我对连接到DB检查运行简单测试时,我在NUnit中收到错误:
[Test]
public void TestConn()
{
string connectionString = ConfigurationManager.ConnectionStrings["FertigungRead"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
Assert.AreEqual(ConnectionState.Open, connection.State);
connection.Close();
}
Run Code Online (Sandbox Code Playgroud)
System.NullReferenceException:未将对象引用设置为对象的实例.
在线:
connectionString = ConfigurationManager.ConnectionStrings["FertigungRead"].ConnectionString;
Run Code Online (Sandbox Code Playgroud)
我可以在测试中使用ConfigurationManager吗?
我有一个引用System.Configuration- 并且ConfigurationSettings发现没有问题 - 但找不到类型或命名空间ConfigurationManager!?
我已经阅读过 - 看起来它应该属于与ConfigurationSettings类相同的命名空间.
编辑:请注意 - 我已经清楚地说我添加了对System.Configuration的引用.已有4条关于此的评论.
c# configurationmanager reference system.configuration .net-3.5
我正在开发一个ASP.NET MVC3 Web应用程序(不是我编写的),其最大上载大小为100MB.现在,此Web应用程序已安装在客户端的服务器计算机上,因此,如果可以为每个客户配置此值最大上载大小,那将会很好.如果需要,他们可以访问Web应用程序的web.config.
现在web.config中有一个值,如下所示:
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="104857600" />
</requestFiltering>
</security>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
此外还有另一个值似乎相似:
<system.web>
<httpRuntime maxRequestLength="104857600" executionTimeout="360" />
</system.web>
Run Code Online (Sandbox Code Playgroud)
104857600字节似乎是100MB文件上载限制.但是,在更改值时,我发现这不是权威值,并且它不遵守新限制.因此,在进行了一些挖掘后,我发现C#代码中的其他位置是硬编码值public const double MaxContentSize = 104857600,另一个C#方法使用该值来接受/拒绝Ajax文件上载.
所以我想我想做的是替换代码中的硬编码数字,以便从web.config中的值读取.然后,至少任何人都可以在部署网站时在web.config中更改该值.
你能做这样的事吗?
MaxContentSize = ConfigurationManager.systemWeb.httpRuntime['maxRequestLength'];
Run Code Online (Sandbox Code Playgroud)
我在web.config中看过一些使用appSettings的例子,例如
<appSettings><add key="MySetting" value="104857600" /></appSettings>
Run Code Online (Sandbox Code Playgroud)
然后访问它像:
ConfigurationManager.AppSettings["MySetting"]
Run Code Online (Sandbox Code Playgroud)
但这意味着在那里添加自定义值,现在我们在web.config中有3个位置可以更改它.任何人都知道如何正确地做到这一点?
非常感谢
我写了一个小实用程序,允许我为另一个应用程序的App.config文件更改一个简单的AppSetting,然后保存更改:
//save a backup copy first.
var cfg = ConfigurationManager.OpenExeConfiguration(pathToExeFile);
cfg.SaveAs(cfg.FilePath + "." + DateTime.Now.ToFileTime() + ".bak");
//reopen the original config again and update it.
cfg = ConfigurationManager.OpenExeConfiguration(pathToExeFile);
var setting = cfg.AppSettings.Settings[keyName];
setting.Value = newValue;
//save the changed configuration.
cfg.Save(ConfigurationSaveMode.Full);
Run Code Online (Sandbox Code Playgroud)
这种效果很好,除了一个副作用.新保存的.config文件丢失所有原始XML注释,但仅在AppSettings区域内.是否可以从原始配置文件AppSettings区域保留XML注释?
c# xml configurationmanager configuration-files xml-comments
我是VS 3.5的新手,我正在编写一个控制台应用程序,需要从app.config文件中提取一些键值.
我有:
Imports System.Configuration
Run Code Online (Sandbox Code Playgroud)
我试图引用,ConfigurationManager.AppSettings但这会产生一个错误:
名称ConfigurationManager未声明
我错过了什么?
在我的几个项目的解决方案中,我试图将PlatformAny CPU 更改为x86.但是x86不在下拉列表中.为了能够选择x86,我需要做什么?
我需要更改为x86,因为System.BadImageFormatException这个问题:构造System.Data.SQLite.SQLiteConnection时导致System.BadImageFormatException的原因是什么

并且,在一个可能相关的问题中,解决方案中的最新项目缺少配置下拉列表中的一个配置:

它应该像所有其他项目一样具有"暂存"配置,但不是.我该如何添加?
编辑:
如果我选择"新建..."选项,那么它会要求我从"任何CPU"复制.如果我从"任何CPU"复制,是不是与首先使用"任何CPU"相同?

使用VS2010
我在web.config中有以下内容(删除了详细信息).
<system.serviceModel>
<behaviors />
<services />
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
<bindings />
<client />
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)
我想使用属性configSource,就像appSettings可以用来从另一个配置文件中获取这些元素的细节一样.
我试图将configSource属性应用于system.serviceModel或每个子节点.但是,我得到了无效的蓝色线条说:
The 'configSource' attribute is not allowed
Run Code Online (Sandbox Code Playgroud)
我在这个问题中提到了第二个答案(汤姆兄弟),这个问题展示了我想要的东西.
web.config可以从外部xml文件中读取吗?
附加
以下是该帖子的配置.有无效的蓝色波浪线.
<connectionStrings configSource="web\config\connectionStrings.config" />
<appSettings configSource="web\config\appSettings.config" />
<system.diagnostics configSource="web\config\diagnostics.config" />
<system.serviceModel>
<bindings configSource="web\config\serviceModelBindings.config" />
<behaviors configSource="web\config\serviceModelBehaviors.config" />
<services configSource="web\config\serviceModelServices.config" />
<client configSource="web\config\serviceModelClient.config" />
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,如何使用configSource属性?
c# ×5
.net ×3
app-config ×3
asp.net ×2
web-config ×2
.net-3.5 ×1
caching ×1
iis-7 ×1
nunit ×1
performance ×1
reference ×1
xml ×1
xml-comments ×1