我有一个程序A,它还有一个app.config文件,我在其中添加了一些密钥,如服务器地址,用户名和密码,用于连接服务器.它是一个控制台应用程序.现在我想制作一个我已经完成的UI.在那个UI中,我想修改程序A的app.config的内容.我怎么做?
这是我尝试的,我将UI(基本上是.exe)复制到程序A的目录,app.config也驻留在该目录中.然后在UI中,我使用ConfigurationManager类的OpenExeConfiguration方法,并将程序A的文件名作为参数传递.但它抛出类型System.Configuration.ConfigurationErrorsException的异常.
所以我认为我的方法不正确.我该怎么办?
编辑:哦,我忘了告诉我正在使用C#,.NET 3.5和VS 2008(如果有帮助:D)
我正在使用C#.NET 2.0 Windows应用程序.
我正在使用app.config进行应用程序设置.
但AppSettings中的更改并未反映运行时,需要重新启动应用程序.
我怎么能避免它.
这是我用来读取和编写应用程序设置的代码片段.
我正在读这样的设置
string temp = ConfigurationManager.AppSettings.Get(key);
Run Code Online (Sandbox Code Playgroud)
我正在更新这样的值,其中node是当前配置/ appSettings节点
node.Attributes["value"].Value = value;
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
Run Code Online (Sandbox Code Playgroud) 我正在使用VS 2008.每次运行我的应用程序时,我都会收到一个PopUp.
以下是PopUp:
以下模块是在启用优化或没有调试信息的情况下构建的:
Run Code Online (Sandbox Code Playgroud)C:\Windows\Microsoft.Net\Framework\v2.0.50727\Temporary ASP.NET Files\root\7c06d97f\c871fca3\assembly\dl3\1ed1f335\00d7b454_9450ca01\BArcodingImaging.DLL要调试此模块,请将其项目构建配置更改为调试模式.要禁止显示此消息,请禁用"在启动时警告无用户代码"调试器选项.
我已经尝试了谷歌上的所有链接来摆脱这个错误,但没有任何作用.实际上大多数链接都是针对VS 2005的.但我使用的是VS 2008.
我用下面的参考: http://social.msdn.microsoft.com/Forums/en-US/tfsbuild/thread/1946cf16-ae70-4394-9cd9-9d35f3f012ed/ ,一个在代码大师.

我正在尝试学习如何使用ConfigurationSection类.我曾经使用IConfigurationSectionHandler,但发布它已被折旧.因此,作为一个好孩子,我正在尝试"正确"的方式.我的问题是它总是返回null.
我有一个控制台应用程序和DLL.
class Program
{
static void Main(string[] args)
{
StandardConfigSectionHandler section = StandardConfigSectionHandler.GetConfiguration();
string value = section.Value;
}
}
Run Code Online (Sandbox Code Playgroud)
app配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="ConfigSectionGroup">
<section name="ConfigSection" type="Controller.StandardConfigSectionHandler, Controller" />
</sectionGroup>
</configSections>
<ConfigSectionGroup>
<ConfigSection>
<test value="1" />
</ConfigSection>
</ConfigSectionGroup>
</configuration>
Run Code Online (Sandbox Code Playgroud)
DLL中的section处理程序:
namespace Controller
{
public class StandardConfigSectionHandler : ConfigurationSection
{
private const string ConfigPath = "ConfigSectionGroup/ConfigSection/";
public static StandardConfigSectionHandler GetConfiguration()
{
object section = ConfigurationManager.GetSection(ConfigPath);
return section as StandardWcfConfigSectionHandler;
}
[ConfigurationProperty("value")]
public string Value
{
get …Run Code Online (Sandbox Code Playgroud) 
我无法从App.Config文件中获取connectionString ..
我错过了什么吗?
在创建类之后,我将类移到了某个文件夹中......问题是什么?没有移动课程的解决方案是什么?
我想在程序中读/写(并保存)应用程序的配置文件
app.config是这样的:
<configuration>
<configSections>
<section name="AdWordsApi" type="System.Configuration.DictionarySectionHandler" requirePermission="false"/>
</configSections>
<AdWordsApi>
<add key="LogPath" value=".\Logs\"/>
...
</AdWordsApi>
</configuration>
Run Code Online (Sandbox Code Playgroud)
当我使用ConfigurationManager.GetSection读取app.config时,它可以工作:
var adwords_section = (System.Collections.Hashtable) System.Configuration.ConfigurationManager.GetSection("AdWordsApi");
Console.WriteLine((string)adwords_section["LogPath"]);
Run Code Online (Sandbox Code Playgroud)
但是当我使用ConfigurationManager.OpenExeConfiguration时:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
ConfigurationSection section = config.GetSection("AdWordsApi");
Console.WriteLine(section["LogPath"]);
Run Code Online (Sandbox Code Playgroud)
我总是得到这个错误:
由于其保护级别,'System.Configuration.ConfigurationElement.this [System.Configuration.ConfigurationProperty]'无法访问
但据我所知,GetSection无法在程序运行时保存配置,就像我在开头说的那样:我想在程序运行时保存配置,所以我必须使用OpenExeConfiguration.
我已经google了很长时间,我发现使用AppSettings,但我使用的是自定义部分..
任何人都可以解释为什么会出现"ConfigurationProperty无法访问"错误?谢谢
编辑:
我已复制本地的系统和System.Configuration至真
.net c# configurationmanager app-config configurationsection
我在使用ConfigurationManager读取app.config时遇到困难.我有一个自定义部分,它使用ConfigurationElementCollection.
我的(缩减)XML:
<configuration>
<configSections>
<sectionGroup name ="CustomerMatching">
<section name="SearchWeight" type="BLL.Contracts.Helpers.CustomerMatching.SearchWeightSection, BLL.Contracts"/>
</sectionGroup>
</configSections>
<CustomerMatching>
<SearchWeight>
<methods>
<method SearchMethod="ByContactName" Weight="100"/> <!--Line 53, referenced in Error -->
<method SearchMethod="ByBusinessName" Weight="250"/>
<method SearchMethod="ByPostcode" Weight="250"/>
<method SearchMethod="ByMobile" Weight="500"/>
<method SearchMethod="ByLandline" Weight="500"/>
<method SearchMethod="ByCompCharNo" Weight="850"/>
</methods>
</SearchWeight>
</CustomerMatching>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我的配置类:
public class SearchWeightSection: ConfigurationSection
{
[ConfigurationProperty("methods", IsRequired = true)]
[ConfigurationCollection(typeof(SearchMethods), AddItemName = "method", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public SearchMethods SearchMethods
{
get { return (SearchMethods) base["methods"]; }
}
}
public class SearchMethods: ConfigurationElementCollection
{
protected override …Run Code Online (Sandbox Code Playgroud) 我有一个ac#assembly,它使用app.config来存储它的数据库连接字符串.在调试应用程序时,我注意到与数据库的连接保持失败,因为ConfigurationManager保持返回machine.config连接字符串:
data source =.\ SQLEXPRESS; 综合安全; ....
我<clear/在app.config中的连接字符串之前添加了>,它修复了我的开发机器上的问题.我将其部署到生产环境时返回了问题.有人能告诉我如何停止使用machine.config连接字符串吗?
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[0].ConnectionString);
<connectionStrings>
<clear/>
<add name="VersionConnectionString"
connectionString=" Data Source=localhost;Initial Catalog=VersionInfo;User ID=user;Password=password"
providerName="System.Data.SqlClient" />
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)
UPDATE
以下仍然给我machine.config连接字符串?!
Configuration appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
string dllConfigData =
appConfig.ConnectionStrings.ConnectionStrings[0].ConnectionString;
Run Code Online (Sandbox Code Playgroud) 背景:
我有一些数据存储在大约100个Web应用程序的web.config文件中.此数据逐渐转移到数据库.网页将显示web.config数据,直到有人点击"编辑"链接,在这种情况下,他们将被重定向到一个网页,这将允许他们更新这些数据,而不是将其保存在数据库中.
问题:
并非所有数据都将在此页面上更改,并将其保存到数据库中.当有人点击"编辑"链接时,我希望表单填充来自web.config文件的数据,当他们点击"保存"时,它会保存到数据库中.但是,使用配置管理器我只能从当前应用程序的web.config文件中提取数据.
问题:
../{dynamic_app_id}/web.config?我正在研究ASP.NET MVC项目,并决定将一些功能提取到一个单独的类库项目(在同一个解决方案中).
我有些感动的元素<appSettings></appSettings>从Web.config主项目(我们称之为项目A),以App.config在类库项目(我们称之为项目B).我添加了对System.Configuration.dll的引用以访问项目B中的ConfigurationManager.
示例App.config文件用于项目B,如下所示.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Key1" value="Value1"/>
<add key="Key2" value="Value2"/>
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我按如下方式访问这些设置(在项目B中).
string key1 = ConfigurationManager.AppSettings["Key1"];
string key2 = ConfigurationManager.AppSettings["Key2"];
Run Code Online (Sandbox Code Playgroud)
我注意到了key1和key2返回的值ConfigurationManager是null.
在调试时,我看到项目A中ConfigurationManager原始的拉取值Web.config(另一<appsettings></appsettings>部分是我没有移动到项目B的部分)!
这对我没有任何意义.有人能告诉我我做错了什么,所以我可以访问App.config中的设置吗?
谢谢.
c# ×8
.net ×5
app-config ×4
appsettings ×1
asp.net ×1
asp.net-mvc ×1
debugging ×1
web-config ×1