Tag: configurationmanager

如何读取app.config文件中定义的basicHttpBinding

我试图弄清楚如何阅读如何读取app.config文件中定义的basicHttpBinding部分。 我尝试这样做,就像我做了端点地址,但它不起作用。 以下是我执行端点地址的方法: private EndpointAddress EndPointAddy { get { var config = ConfigurationManager.GetSection(“system.serviceModel/Client”) as ServiceModelSectionGroup; foreach (ChannelEndpointElement endpoint in config.Client.Endpoints) { Uri address = endpoint.Address; if (address != null) { return new EndpointAddress(address); } } return null; } } 这是我用BasicHttpBinding尝试过的 private BasicHttpBinding Binding { get { var config = ConfigurationManager.GetSection(“system.serviceModel/Client”) as ServiceModelSectionGroup; foreach (ChannelEndpointElement bindings […]

ConfigurationManager.save()失败

我正在编写一个C#应用程序(没有WinForms)。 阅读了几个关于ConfigurationManager的线程后,我结束了以下代码: public static string GetValue(string key) { Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var settings = configFile.AppSettings.Settings; if (settings[key] == null) throw new ArgumentException(“Requested unknown key from Application Configuration(“+configFile.FilePath+”)”,key); System.Console.Out.WriteLine(“Got “+ key + ” = ” + settings[key].Value); return settings[key].Value; } public static void SetValue(string key, string value) { try { Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var […]

来自自定义文件的AppSettings

我正在努力配合.NET 2.0中的配置和设置类 如果以下内容包含在名为app.config的文件中 我知道我可以访问appSetting by // this returns “Hello World!” ConfigurationManager.AppSettings[“Foo”] 但是,如果文件名为app1.config (或任何其他名称),则无法访问appSetting。 只要我理解,使用ConfigurationManager.OpenExeConfiguration我应该阅读自定义配置设置文件。 Configuration conf = ConfigurationManager.OpenExeConfiguration(@”..\..\app1.config”); // this prints an empty string. Console.WriteLine(conf.AppSettings.Settings[“Foo”]); 但是conf.AppSettings.Settings[“Foo”]返回一个空字符串。 我也试过以下代码但没有成功 ExeConfigurationFileMap exeFileMap = new ExeConfigurationFileMap(); exeFileMap.ExeConfigFilename = System.IO.Directory.GetCurrentDirectory() + “\\App1.config”; Configuration myConf = ConfigurationManager.OpenMappedExeConfiguration (exeFileMap, ConfigurationUserLevel.None); // returns empty string as well Console.WriteLine(myConf.AppSettings.Settings[“Foo”]); 如何从不称为app.config的文件中读取设置?

如何使用C#检索.config文件中的自定义配置节列表?

当我尝试使用时检索.config文件中的部分列表 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); config.Sections集合包含一堆系统部分,但我没有在configSections标记中定义文件的部分。

ConfigurationManager Save()想要创建一个tmp-File

我遇到了想要写入.exe.config的应用程序的问题。 请参阅以下代码: using System; using System.Configuration; using System.IO; namespace ConfigurationManagerError { class Program { static void Main(string[] args) { try { // set Config-File to persistent folder ExeConfigurationFileMap exeMap = new ExeConfigurationFileMap(); exeMap.ExeConfigFilename = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), “ConfigError\\ConfigurationManagerError.exe.config”); Configuration config = ConfigurationManager.OpenMappedExeConfiguration(exeMap, ConfigurationUserLevel.None); config.AppSettings.Settings.Add( new KeyValueConfigurationElement(“TestKey”, “TestValue”)); config.Save(ConfigurationSaveMode.Modified); Console.WriteLine(“Successfully write of Configuration-File to ” + config.FilePath); […]

AppSettings的变化需要重新启动我的应用程序如何避免?

我正在使用C#.NET 2.0 Windows应用程序。 我正在使用app.config进行应用程序设置。 但AppSettings中的更改并未反映运行时,需要重新启动应用程序。 我怎么能避免它。 这是我用来读取和编写应用程序设置的代码片段。 我正在读这样的设置 string temp = ConfigurationManager.AppSettings.Get(key); 我正在更新这样的值,其中node是当前配置/ appSettings节点 node.Attributes[“value”].Value = value; xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

尝试读取连接字符串的NullReferenceException

我有两个项目。 一个是网络,另一个是Windows窗体。 Web项目连接到数据库,但Windows项目抛出exceptionNullReferenceException读取连接字符串。 我使用相同的类来连接两个项目。 使用LINQTOSQL建立连接。 这是我的连接字符串: 这就是我读它的方式。 string CS = ConfigurationManager.ConnectionStrings[“GPSystemConnectionString”].ConnectionString; (此行发生exception)…..(对象引用未设置为对象的实例。) 注意:我使用相同的类连接两个项目….一个连接但另一个失败。 请任何人帮助我! 先感谢您。

使用configurationmanager从多个web.config文件中读取

背景: 我有一些数据存储在大约100个Web应用程序的web.config文件中。 此数据将逐渐移至数据库。 网页将显示web.config数据,直到有人点击“编辑”链接,在这种情况下,他们将被重定向到一个网页,这将允许他们更新这些数据,而不是将其保存在数据库中。 问题: 并非所有数据都将在此页面上更改,以便将其保存到数据库中。 当有人点击“编辑”链接时,我希望表单填充来自web.config文件的数据,当他们点击“保存”时,它会保存到数据库中。 但是,使用配置管理器我只能从当前应用程序的web.config文件中提取数据。 问题: 有没有办法使用configurationmanager来选择web.config文件,比如说../{dynamic_app_id}/web.config ? 正在阅读它们作为普通的xml文件我唯一的选择? 这种方法有什么缺陷吗? 还有其他解决方案可能更好吗?

WPF中的ConfigurationManager

我在wpf项目中有一个配置文件来存储connectionstring。 但是当我尝试获取AppSettings和ConnectionStrings时,我得到null。 WEB.config文件是这样的: 我试过几种方式: W1: ConnStr = ConfigurationManager.ConnectionStrings[“Trackboard”].ConnectionString; W2: ConnStr = ConfigurationManager.ConnectionStrings[0].ConnectionString; W3: ConnStr = ConfigurationManager.AppSettings[“Trackboard”]; W4: ConnStr = ConfigurationManager.AppSettings[0]; 他们都没有工作。 但这个工作: ConnStr = @”Data Source=(localdb)\v11.0;Initial Catalog=Trackboard;Integrated Security=True;AttachDbFileName=E:\Users\Sean\Workspace\DATABASE\Trackboard.mdf”; (这意味着我不能使用配置文件,这违背我的意愿)我需要帮助。

由于其保护级别,ConfigurationProperty无法访问

我想在程序中读/写(并保存)应用程序的配置文件 app.config是这样的: … 当我使用ConfigurationManager.GetSection读取app.config时,它可以工作: var adwords_section = (System.Collections.Hashtable) System.Configuration.ConfigurationManager.GetSection(“AdWordsApi”); Console.WriteLine((string)adwords_section[“LogPath”]); 但是当我使用ConfigurationManager.OpenExeConfiguration时 : Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal); ConfigurationSection section = config.GetSection(“AdWordsApi”); Console.WriteLine(section[“LogPath”]); 我总是得到这个错误: 由于其保护级别,’System.Configuration.ConfigurationElement.this [System.Configuration.ConfigurationProperty]’无法访问 但据我所知, GetSection无法在程序运行时保存配置,就像我在开头说的那样:我想在程序运行时保存配置,所以我必须使用OpenExeConfiguration 。 我已经google了很长时间,我发现使用AppSettings,但我使用的是自定义部分.. 任何人都可以解释为什么会出现“ConfigurationProperty无法访问”错误? 谢谢 编辑: 我已将System和System.Configuration的 copy local设置为true