app-config 相关问题

.NET应用程序配置文件包含特定于应用程序的设置。

以编程方式将密钥添加到 App.config C#

我正在尝试以编程方式添加密钥并将其保存到我的 App.config 文件中。我的代码如下,我见过很多这样的不同示例,但我的代码不起作用。我正在尝试添加一个全新的密钥...

回答 3 投票 0

如何首先覆盖 EF 代码中的“不可见”连接字符串?

我首先通过代码使用 EF 6.1.2 编写了我的应用程序。输入命令“更新数据库”后,数据库已在我的 Microsoft SQL Server 2017 中创建。到目前为止一切顺利。然而我从来没有...

回答 1 投票 0

如何将环境变量列表绑定到.Net中的选项

我在托管我的应用程序的 kubernetes pod 上有一个环境变量列表,它没有任何部分,只有如下所示的变量列表: KAFKA_BOOTSTRAP_SERVERS 卡夫卡_PAS...

回答 1 投票 0

在应用程序服务配置中设置和使用 web.config 中不存在的变量名称

使用 .NET Framework 4.8 作为网站的 API 应用程序。 //网页配置 //主项目解决方案中的代码,这有效 变量 ID =

回答 1 投票 0

如何在配置文件中添加 json 字符串作为应用程序设置值

我有一个要求,在进行 API 调用之前需要获取一组 json。 我计划在 app.config 中添加这些 json 字符串,如下所示 我有一个要求,在进行 API 调用之前需要获取一组 json。 我计划在 app.config 中添加这些 json 字符串,如下所示 <add key="Jsons" value="{""Id"":""25"",""Name"":""Value-1""}"/> 但是添加此内容会导致在值的开头出现编译错误“缺少空格”。 如果我遗漏了什么,请告诉我。 我不想创建一个单独的文本文件来读取 json。这就是为什么我决定使用 app.config 本身 您的报价格式不正确。 你能试试这个吗: <add key="Jsons" value='{"Id":"25","Name":"Value-1"}'/> app.config 仍然是 XML!您需要对引号使用 XML 转义序列。 <add key="Jsons" value="{&quot;Id&quot;:&quot;25&quot;,&quot;Name&quot;:&quot;Value-1&quot;}"/> 我在这里看到两个选择: 使用“\”转义: <add key="Jsons" value="{\"Id\":\"25\",\"Name\":\"Value-1\"}"/> 使用单引号: <add key="Jsons" value="{'Id':'25','Name':'Value-1'}"/> 如何在应用程序配置中获取动态值?可以吗? 应用程序设置 添加 key="AesKey" value="vSdLqF9X7SGPybZRGNDvDOwXOj7oaZA02YHhzcJICHO=" //代替值,我需要传递 crm 实体中可用的现有配置值。

回答 4 投票 0

.net 4.8 控制台应用程序的 Nlog 配置写入数据库 - 未写入日志

我有一个 C#.Net 4.8 控制台应用程序,我尝试使用 NLog 记录到 MSSQL 数据库。 我的应用程序具有以下内容: 私有静态记录器 nLogger = LogManager.GetLogger("

回答 1 投票 0

自动绑定重定向替换整个 App.config 文件

我在我的 C# 项目中添加了一个 App.config 文件,其中包含很多内容:appSettings、connectionStrings、entityFramework、system.net mailSettings 等。 当我构建项目时,结果 <

回答 1 投票 0

如何加密 .NET Core 5.0 的 App.config 中的部分或密钥?

我制作了一个发送电子邮件的程序,需要将密码存储在App.config文件中,但我不希望仅通过打开文件即可访问密码。我想加密一些...

回答 1 投票 0

如何可怕地扭曲 C# 项目来读取其类库的 app.config 文件

所以让我首先说“我知道这不是最佳实践”并且我不想将 app.config 文件中的信息添加到 web.config 文件中......我已经一个项目...

回答 1 投票 0

类似于 .NET 的 Spring 云配置解决方案

Spring Cloud Config 允许存储和提供应用程序的配置,多重部署到分布式环境。有 .NET 替代方案吗?所以在 app.config 中我可以说 - 获取配置,或者

回答 2 投票 0

在同一个App.Config中使用ConnectionStrings和自定义ConfigSections

我有一个按预期工作的自定义 configSection。但是,当我添加“connectionStrings”部分时,我收到错误: 配置系统初始化失败 在线的: StencilObjects 所以 =

回答 2 投票 0

Windows 窗体应用程序的设置值存储在哪里?

我有一个 Windows 窗体应用程序。当我在项目属性的“设置”选项卡上查看设置以及查看 App.config 时,我会看到一组值。当我查看某些

回答 2 投票 0

如何更新现有的 AWS AppConfig 配置文件数据?

假设我的应用程序配置中有一个托管配置版本: { “配置”:[ { “一”:1, “乙”:2 }, { “C”:3, “D&q...

回答 1 投票 0

如何在配置文件中设置路径

我有一个运行脚本的测试。它从 app.config 文件获取路径: 我有一个运行脚本的测试。它从 app.config 文件获取路径: <scriptsSectionGroup> <scriptsCollection> <scripts> <add name="TestScript" path="C:\My Projects\RestServicePublishing\UtilityFixture\testScript.bat"/> </scripts> </scriptsCollection> </scriptsSectionGroup> “testScript.bat”位于UtilityFixture 项目中。它不在子文件夹中。 实用夹具 测试脚本.bat 但是我不想像这里一样指定完整路径。我想让它在项目“UtilityFixture”中找到“testScript.bat”,无论项目位于哪个目录。假设其他人将其从源代码管理下载到其他位置。 为了进一步解释这一点,如果我说将“RestServicePublishing 解决方案移动到一个新文件夹(例如“C:\My Projects 2”),我希望找到该文件。 有人可以告诉我这是否可行吗? 谢谢 如果脚本与可执行文件位于同一文件夹中,则根本不需要指定路径。否则,您可以相对于 EXE 指定它, ..\testScript.bat # in parent folder scripts\testScript.bat # in subfolder 无论如何,你是对的,你当然不想使用“众所周知”的位置(即硬编码路径)。

回答 1 投票 0

如何从 .NET 中的 app.config 检索自定义部分?

我有一个.NET Framework 4.7.2库项目,里面有一个像这样的App.config文件: 我有一个.NET Framework 4.7.2库项目,里面有一个像这样的App.config文件: <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="NewDocumentMetadata" type="System.Configuration.NameValueSectionHandler" /> </configSections> <NewDocumentMetadata> <add key="Type" value="principal"/> <add key="IsActive" value="true"/> </NewDocumentMetadata> <appSettings> <add key="Entity" value="9"/> <add key="Flux" value="pdf"/> </appSettings> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <!-- [...] --> </assemblyBinding> </runtime> </configuration> 如您所见,我有一些标准设置,但也有一个自定义部分。我的设置没有问题,但是当我检索该部分时,它可以工作,但是我被卡住了,当我尝试将其转换为 NameValueCollection 或 AppSettingsSection 时,它给了我一个 null 值,我'我被一个无法使用的 ConfigurationOption 对象困住了。 var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location); flux = appConfig.AppSettings.Settings["Flux"].Value; //Works entity = appConfig.AppSettings.Settings["Entity"].Value; //Works var metadataSection = appConfig.GetSection("NewDocumentMetadata"); //What do I do with this boy? 我需要检索NewDocumentMetadata部分中的设置,如何进行? 你应该试试这个: var metadataSection = ConfigurationManager.GetSection("NewDocumentMetadata") as NameValueCollection; // Get all the value foreach key foreach(var key in metadataSection.AllKeys) { string value = metadataSection.GetValues(key).FirstOrDefault() } 我刚刚发现问题出在这一行: <section name="NewDocumentMetadata" type="System.Configuration.NameValueSectionHandler" /> 我改变了类型并选择了这个: <section name="NewDocumentMetadata" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> 现在我只需要执行这种代码即可检索我想要的值: var metadataSection = (AppSettingsSection)appConfig.GetSection("NewDocumentMetadata"); foreach (var key in metadataSection.Settings.AllKeys) { string value = metadataSection.Settings[key].Value; }

回答 2 投票 0

如何从另一个程序集读取app.config?

我有两个项目: 控制台项目(Test.exe) 类库项目(Test.Data.dll) 我的类库项目包含一个 app.config 文件。 <

回答 4 投票 0

C# Windows 窗体:实用地编辑/更新生成的 App.Config 文件中的字符串数组

我需要务实地更新 Windows 窗体自动生成的 app.config 文件。 具体来说是一个 ArrayOfStrings。 期望的输出: 我需要务实地更新 Windows 窗体自动生成的 app.config 文件。 具体来说是一个 ArrayOfStrings。 期望的输出: <setting name="Test" serializeAs="Xml"> <value> <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <string>Test 1</string> <string>Test 2</string> <string>Test 3</string> </ArrayOfString> </value> </setting> What my code generates: <setting name="Test" serializeAs="Xml"> <value>New Data 3</value> </setting> 我研究过Xml序列化,但它对我来说是全新的,我正在努力弄清楚它。 公共无效saveCardDataToSettings() { List<String> DetectorCardTypeData = new List<string>(); foreach (DataGridViewRow row in dgvDetectorCardData.Rows) { string CurrentRowData = (row.Cells[0].Value + "," + row.Cells[2].Value + "," + row.Cells[4].Value); DetectorCardTypeData.Add(CurrentRowData); } StringCollection TestData = new StringCollection(); foreach (string CardData in DetectorCardTypeData) { TestData.Add(CardData); } //Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath); Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConfigurationSectionGroup applicationSectionGroup = config.GetSectionGroup("applicationSettings"); ConfigurationSection applicationConfigSection = applicationSectionGroup.Sections["EXP_Battery_Calculator.Properties.Settings"]; ClientSettingsSection clientSection = (ClientSettingsSection)applicationConfigSection; SettingElement appData = clientSection.Settings.Get("Test"); foreach (string WriteME in TestData) { appData.Value.ValueXml.InnerText = WriteME; clientSection.Settings.Add(appData); } applicationConfigSection.SectionInformation.ForceSave = true; config.Save(); listBox1.DataSource = TestData; } 仍然无法理解为什么需要在应用程序配置中存储数据,但无论如何。 首先,在您的设置文件中创建一个新参数。根据需要将其命名(下面示例中的“StoredData”),将类型设置为System.Collections.Specialized.StringCollections,设置范围“Application”并添加默认值(我添加了“Empty”)。这应该会生成一个 XML 字符串: <?xml version="1.0" encoding="utf-16"?> <ArrayOfString xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <string>Empty</string> </ArrayOfString> 并且还将修改默认配置: <?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=b77a5c561934e089" > <section name="YourNamespace.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </sectionGroup> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" /> </startup> <applicationSettings> <YourNamespace.Properties.Settings> <setting name="StoredData" serializeAs="Xml"> <value> <ArrayOfString xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <string>Empty</string> </ArrayOfString> </value> </setting> </YourNamespace.Properties.Settings> </applicationSettings> </configuration> 要编辑 ArrayOfString 节点,我使用了以下代码: // Diving through config nodes to <value> element var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (config.SectionGroups["applicationSettings"] is not { } appSettings) return; if (appSettings.Sections.OfType<ClientSettingsSection>().FirstOrDefault(section => section.Settings.Get(nameof(Properties.Settings.Default.StoredData)) != null) is not { } settingsSection) return; if (settingsSection.Settings.Get(nameof(Properties.Settings.Default.StoredData))?.Value?.ValueXml is not { } storedData) return; // Removing everyting from <ArrayOfString> (including it) storedData.RemoveAll(); // Initializing XmlDocument instance to create new nodes var xDoc = new XmlDocument(); // Manually creating parent node <ArrayOfString xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> var xArrayOfString = xDoc.CreateElement("ArrayOfString"); xArrayOfString.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema"); xArrayOfString.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); // Just test collection of strings var testData = Enumerable.Range(0, 10).Select(x => x.ToString()); // Looping through records of DataGridView, which should already been "stringified" // Creating nodes <string>...</string> where "..." is string representation of each DataGridView row (in a way to want to store it) foreach (var testDataItem in testData) { var xString = xDoc.CreateElement("string"); xString.InnerText = testDataItem; xArrayOfString.AppendChild(xString); } // And appending new rebuilded <ArrayOfString> to a <value> node in config storedData.AppendChild(storedData.OwnerDocument.ImportNode(xArrayOfString, true)); // And just saving config.Save(ConfigurationSaveMode.Minimal, true); 那个例子,在 config.Save 调用之后立即重写配置并产生这样的外观: <?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=b77a5c561934e089" > <section name="YourNamespace.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </sectionGroup> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" /> </startup> <applicationSettings> <YourNamespace.Properties.Settings> <setting name="StoredData" serializeAs="Xml"> <value> <ArrayOfString xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <string>0</string> <string>1</string> <string>2</string> <string>3</string> <string>4</string> <string>5</string> <string>6</string> <string>7</string> <string>8</string> <string>9</string> </ArrayOfString> </value> </setting> </YourNamespace.Properties.Settings> </applicationSettings> </configuration> 但是它不会在运行时更新Property.Settings.Default.StoredData属性。只有在应用程序重新启动后,它才会从配置中加载修改后的数据。它也不会修改项目文件夹中的“App.config”。输出二进制文件附近只有“YourAssemblyName.exe.config”。 不确定这种方式是否合适或适用。仍然不明白为什么你决定以这种方式惩罚小配置文件。您对此有用户范围的设置。或者只是在应用程序目录中编写自己的单独文件,例如“myStoredData.xml”。

回答 1 投票 0

覆盖配置设置

我有一个在多个项目中使用的配置文件,general.config,如下所示: ...

回答 4 投票 0

如何查找活动app.config文件的路径?

我正在尝试完成这个异常处理程序: if (ConfigurationManager.ConnectionStrings["ConnectionString"]==null) { 字符串路径ActiveConfigFile = ...? 抛出新的 ConfigurationErrorsExcep...

回答 8 投票 0

.Net 4.6 AppContextSwitchOverrides 未设置配置中声明的开关

我正在尝试从 app.config(或 web.config)文件中的条目设置兼容性开关“Switch.System.Xml.IgnoreEmptyKeySequences”,但覆盖似乎被忽略。 为了雷...

回答 4 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.