app-config

Is it possible to modify configuration ConnectionStrings at runtime?

人走茶凉 提交于 2019-12-01 12:14:19
Is it possible to modify the connectionstrings defined in the app.config/web.config at runtime? I want to use different configfiles depending on the machine the app/site is run on (only for debugging purposes, of course. We'll use the regular config files when deployed). I can write AppSettings, but not ConnectionStrings (AFAIK). Or can I? Yes it's possible, but AFAIK only via Reflection. The following code should do what you need (read below for usage): public static string SetConnectionString(Type assemblyMember, Type settingsClass, string newConnectionString, string connectionStringKey) {

How to create custom config section in app.config

旧巷老猫 提交于 2019-12-01 11:43:55
I want to add the custom configsection in the app.config file as follows <Companies> <Company name="" code=""/> <Company name="" code=""/> </Companies> <Employees> <Employee name="" Des="" addr="" sal=""/> <Employee name="" Des="" addr="" sal=""/> </Employeess> <Departments> <Department Id="" Projects=""/> </Departments> <Projects> <Project Path=""/> </Projects> In the Department section it is referring to Projects section. Can anybody tell me way to do it? And how to access it in my code? @Bhaskar: Please find the code for your comment. public class RegisterCompaniesConfig :

Is it possible to modify configuration ConnectionStrings at runtime?

[亡魂溺海] 提交于 2019-12-01 11:36:50
问题 Is it possible to modify the connectionstrings defined in the app.config/web.config at runtime? I want to use different configfiles depending on the machine the app/site is run on (only for debugging purposes, of course. We'll use the regular config files when deployed). I can write AppSettings, but not ConnectionStrings (AFAIK). Or can I? 回答1: Yes it's possible, but AFAIK only via Reflection. The following code should do what you need (read below for usage): public static string

Supply App.config file via NuGet

痞子三分冷 提交于 2019-12-01 10:19:28
问题 I have some information in my nupkg that I would like to include in the consumer's application. What is the best way to include a partial app.config directly when consuming the nupkg? I have currently done this: mypackage.nupkg content App.config But this makes that it wants to override a user's own App.config, while there could be valuable information in there. So I want to combine my App.config with the user's App.config. I already saw something like this: C#: manage Multiple App.config

How to get configuration element

别等时光非礼了梦想. 提交于 2019-12-01 05:20:38
Helo Can anybody explain me how to get configuration element from .config file. I know how to handle attributes but not elements. As example, I want to parse following: <MySection enabled="true"> <header><![CDATA[ <div> .... </div> ]]></header> <title> .... </title> </MySection> My c# code looks like this so far: public class MyConfiguration : ConfigurationSection { [ConfigurationProperty("enabled", DefaultValue = "true")] public bool Enabled { get { return this["enabled"].ToString().ToLower() == "true" ? true : false; } } [ConfigurationProperty("header")] public string header { ??? } } It

Create app config file if doesn't exist C#

左心房为你撑大大i 提交于 2019-12-01 04:40:38
When I try to open my app config file by ConfigurationManager.OpenExeConfiguration(filePath); it returns an exception, because there is no file. But I need to create this file in this case. But as I know, config file create by adding at VS like How to: Add an Application Configuration File to a C# Project So, how to create this file in other way? If you really want to create an empty config file at runtime you can do something like this, but note that if the config file already exists this code will overwrite it : System.Text.StringBuilder sb = new StringBuilder(); sb.AppendLine("<?xml version

How do I use .NET custom ConfigurationElement properties on descendent elements?

醉酒当歌 提交于 2019-12-01 04:21:40
How can I get and use an attribute set in the parent ConfigurationSection in the descendent CustomSetting element? I need this attribute when the CustomSetting element is returning the Value property. I want to format the App.config like this: <CustomSettings someProperty="foo"> <CustomSetting key="bar" value="fermeneba" /> <CustomSetting key="laa" value="jubaduba" /> </CustomSettings> I have the code working, except that I cannot find a way to access the someProperty attribute from the CustomSetting class. The only way that I've found, so far, is to format the configuration like this, which

app.configs and MSTest Project - null reference for a connection string

时光总嘲笑我的痴心妄想 提交于 2019-12-01 03:45:50
When I try to run Unit Tests (mstest) I run into this issue. The line of code: _mainCnStr = System.Configuration.ConfigurationManager. ConnectionStrings["main"].ConnectionString; Comes back as a null reference It doesn't do this in the main UI project when I run it. What is the right method to get this connection string setting seen by the Unit Test project? I tried embedded as a resource. I tried Copy Always. What is the right combination of settings that will fix this for me? One thing to watch with MSTest (from the IDE at least); it doesn't run the tests in the regular output (bin) folder,

Actually read AppSettings in ConfigureServices phase in ASP.NET Core

北战南征 提交于 2019-12-01 02:52:50
I need to setup a few dependencies (services) in the ConfigureServices method in an ASP.NET Core 1.0 web application. The issue is that based on the new JSON configuration I need to setup a service or another. I can't seem to actually read the settings in the ConfigureServices phase of the app lifetime: public void ConfigureServices(IServiceCollection services) { var section = Configuration.GetSection("MySettings"); // this does not actually hold the settings services.Configure<MySettingsClass>(section); // this is a setup instruction, I can't actually get a MySettingsClass instance with the

Including a service reference from a class library

寵の児 提交于 2019-12-01 02:46:18
I have a C# class library and a startup project (a console app). The class library includes a service reference to a web service. When I try to run the project, I get an InvalidOperationException because the startup project isn't reading the class library's app.config, and it's ignoring the service reference. To get it working, I'm forced to add the same service reference to the startup project. Is there any way I can avoid this? Can I make the startup project recognize the class library's service reference and app.config without having to copy it to the startup project? I've tried adding a