我对调试和发布设置进行了几处更改,现在我想恢复出厂设置。我怎么能那样做?
我有一个有效的自定义配置部分。但是,通过 a 获取我的数据很痛苦,ConfigurationElementCollection但是当我尝试将我的属性实现为 IEnumerable 时,它失败并显示错误:
ConfigurationErrorsException 未处理“属性 'contacts' 不是 ConfigurationElement。”
这是导致失败的代码:
[ConfigurationProperty("contacts", IsDefaultCollection = false)]
public IEnumerable<string> Contacts
{
get { return ((ContactCollection)base["contacts"]).Cast<ContactElement>().Select(x => x.Address); }
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我将其更改为:
[ConfigurationProperty("contacts", IsDefaultCollection = false)]
public ContactCollection Contacts
{
get { return ((ContactCollection)base["contacts"]); }
}
Run Code Online (Sandbox Code Playgroud)
一切正常。这个答案听起来像是微软决定不允许的东西,所以我不能拥有除ConfigurationElement. 真的是这样吗?如何将我的财产实施为IEnumerable<string>?
以防万一,我正在尝试存储电子邮件,并且我希望每个电子邮件都有一个元素,因为可能有很多电子邮件,我们可能希望将来存储有关每个联系人的更多信息,我认为单个以逗号分隔的列表可能会变得丑陋。例如,类似于:
<emergency>
<contact address="sirdank@stackoverflow.com" />
<contact address="jon.skeet@stackoverflow.com" />
</emergency>
Run Code Online (Sandbox Code Playgroud)
或者
<emergency>
<contact>sirdank@stackoverflow.com</contact>
<contact>jon.skeet@stackoverflow.com</contact>
</emergency>
Run Code Online (Sandbox Code Playgroud)
谢谢!
我正在开发一个类库(.NET Standard 2.0)CL,它将由我们的一些旧项目(.NET Framework 4.6.1)LP和许多新实现(.NET Core 3.1)NI使用。
我的类库 CL 需要一些配置设置,但是当它被新的实现 NI 使用时,CL 会收到Microsoft.Extensions.Configuration.IConfiguration,就像 NI 那样appSettings.json。
当从遗留项目 LP 使用时,CL 接收System.Configuration.ConfigurationManager.AppSettings为 LP 具有 或web.config之一app.config。
是否有任何优雅的方法可以从 .NET Framework 或 .NET Core 两种类型的项目中读取配置?我不想读取 LP 或 NI 等正在使用我的库 CL 的项目中的配置,因为该配置对他们来说没有用。
private static void LoadConfiguration() // called from constructor
{
string receivedFromNETCore = configuration["DataMappingXml"];
// Microsoft.Extensions.Configuration.IConfiguration
string receivedFromNETFramework =
ConfigurationManager.AppSettings["DataMappingXml"];
// TODO: LOAD configuration and process.
}
Run Code Online (Sandbox Code Playgroud) configurationmanager .net-core .net-standard-2.0 .net-4.8 microsoft.extensions.configuration
在创建应用程序或库的配置时,我通常更喜欢在该<appSettings>部分上使用自定义配置部分,原因如下.
鉴于此,何时我想使用该部分的松散类型<add/>键/值机制<appSettings>?我记得,本节中的应用程序级配置可以覆盖machine.config中的现有机器级配置.这是唯一的情况,还是有其他原因?
.net configuration configurationmanager appsettings custom-configuration
当用于连接数据库时,如何解决错误消息"ConfigurationManager not declared"?
以下是给出错误的代码行:
SQLConnStr= ConfigurationManager.ConnectionStrings("SQLConnStr").ConnectionString
Run Code Online (Sandbox Code Playgroud) 创建配置对象以管理.net配置文件时,是否需要关闭文件.
我有一个带有几个.dll组件的应用程序.我是否必须使用全局配置以避免将其他.dll所做的更改写入同一配置文件.
Public Shared config As System.Configuration.Configuration
config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
....do config entries here.
config.save
Run Code Online (Sandbox Code Playgroud)
我是否知道在从另一个模块更改同一配置文件之前需要关闭/处置此配置.
好的,我只是放弃了这一点.
我希望能够使用用户配置文件记录用户首选项,该文件将从应用程序配置文件中引用.我正在尝试使用ConfigurationManager和配置文件.我可以从用户设置中读取得很好,但设置它们是另一个问题.我想将应用程序设置与两个不同文件中的用户设置分开.
我用这个时:
<appSettings file="user.config">
</appSettings>
Run Code Online (Sandbox Code Playgroud)
和user.config看起来像:
<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
<add key="localSetting" value="localVal"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)
我可以使用ConfigurationManager来读取本地设置,但不能保存到文件中.
var oldLocVal = ConfigurationManager.AppSettings["localSetting"];
ConfigurationManager.AppSettings.Set("localSetting", "newLocalValue"); // Doesn't save to file.
Run Code Online (Sandbox Code Playgroud)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="localSetting" value="localVal"/>
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
然后我想以这种方式调用并保存AppSettings:
var uMap = new ConfigurationFileMap("user.config");
var uConfig = ConfigurationManager.OpenMappedMachineConfiguration(uMap);
var oldLocalVarFromConfig = uConfig.AppSettings.Settings["localSetting"]; // NO
uConfig.AppSettings.Settings.Remove("localSetting"); // NO
uConfig.AppSettings.Settings.Add("localSetting", "newValue");
uConfig.Save();
Run Code Online (Sandbox Code Playgroud)
但它不会让我访问配置的应用程序设置.(将某些内容转换为AppSettings时出现问题)
configSource而不是file属性appSettings.
提前致谢.
我有一个普遍的问题,但我也会解释为什么我这么问你可以更好地了解我的意思.
我有一个在设置中定义了web服务URL的DLL,并且在运行时它使用Settings.Default从设置中获取URL.但是,我们的环境都没有(dllName).dll.config文件,并且未在调用应用程序的(exeName).exe.config中定义特定设置.很明显,默认值没有被使用,因为它被设置为某个内部IP地址; 但这在生产中有效,他们没有在我能找到的任何.config文件中定义此设置,并且它仍然以某种方式访问正确的Web服务URL.在这种情况下,我需要知道加载值的位置.
所以我的更广泛的问题是,层次结构如何在.net中加载设置?例如,它首先在machine.config中查找,然后是(exeName).exe.config,如果它是dll,它将转到(dllName).dll.config?它首先看起来在哪里,它在其他地方的顺序是什么,还有其他地方我没有提到可以定义这个配置吗?
此外,对于DLL,如果您在"设置"中定义了某些内容,那么它是否会作为默认值嵌入到已编译的dll中,如果在任何其他.config文件中找不到该属性,那么它是否会被使用?
我想在app.config文件中存储一个sql查询,该文件有<和>字符但文件显示错误'expecting>',你遇到过这个问题吗?
c# configuration configurationmanager app-config configuration-files
在旧版ASP.NET应用程序中,无法通过导航到URL来下载*.config文件.但新的惯例是使用.现在,如果我有一个名为的ASP.NET核心网站,它是由IIS从一个名为的服务目录服务的,并且有一个文件位于......如果有人导航到https, IIS是否足够聪明,知道不通过HTTP提供此文件://contoso.com/appsettings.json?appsettings.jsoncontoso.comC:\inetpub\websites\contoso.comC:\inetpub\websites\contoso.com\appsettings.json
c# ×4
.net ×3
app-config ×2
.net-3.5 ×1
.net-4.8 ×1
.net-core ×1
appsettings ×1
asp.net-core ×1
config ×1
dll ×1
factory ×1
iis ×1
linq ×1
microsoft.extensions.configuration ×1
settings ×1
vb.net ×1