I'm need to read connectionStrings from different files .config
file1.ddl.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="" connectionString=""/>
</connectionStrings>
</configuration>
file2.ddl.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="" connectionString=""/>
</connectionStrings>
</configuration>
I try ConfigurationManager.ConnectionStrings["NAME"].ConnectionString but i recive "Object not set" and i can't use ConfigurationSettings.AppSettings because it´s deprecated
enter image description here
U can try add the key's in the same file.
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location; //exePath
Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
ConnectionStringsSection section = config.GetSection("connectionStrings") as ConnectionStringsSection;
string connectionFile = section.ConnectionStrings["nameConnection"].ConnectionString;
Related
I am very new to app.config files:
In an app.config file which look like this
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="connectionstring" connectionString="Server=server;User ID=bar;Password=foobar" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
I want to split up the connection string into various sections like Server, UserId, etc in a different file and then concat it together in my app.config:
Pseucode
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="connectionstring" connectionString=Server+UserId+Password providerName="System.Data.SqlClient />
</connectionStrings>
</configuration>
Is this possible?
I have MVC5 .NET 4.6.1 C# web application
I want to create a custom config file separate from web.config to store some settings my application uses.
I tried to follow this article https://support.microsoft.com/en-us/kb/815786
however the items I set in app.config:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
</system.web>
<appSettings>
<add key="Key0" value="0" />
<add key="Key1" value="1" />
<add key="Key2" value="2" />
</appSettings>
</configuration>
are not seen in my application see , eg. they come as null:
string attr = ConfigurationManager.AppSettings["Key0"];
Why isn't it working? Am I missing something?
Alternatively I would like to create a custom config file eg. mycustom.config to define my global app settings.
EDIT
Solution I used
Follwing this post https://social.msdn.microsoft.com/Forums/vstudio/en-US/11e6d326-c32c-46b1-a9a2-1fbef96f33ee/howto-custom-configuration-files?forum=netfxbcl
In web.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="newAppSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</configSections>
<newAppSettings file="C:\mycustom.config"/>
</configuration>
Then mycustom.config
<?xml version="1.0" encoding="utf-8" ?>
<newAppSettings>
<add key="OurKey" value="OurValue"/>
</newAppSettings>
And reading the value:
System.Collections.Specialized.NameValueCollection newAppSettings = (System.Collections.Specialized.NameValueCollection)System.Configuration.ConfigurationManager.GetSection("newAppSettings");
string key = Convert.ToDateTime(newAppSettings["OurKey"]);
You can use separate config file for connection strings and app settings:
<appSettings configSource="appSettings.config" />
<connectionStrings configSource="connectionStrings.config"/>
appSettings.config file
<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
<add key="Setting1" value="App setting 1" />
</appSettings>
connectionStrings.config file
<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
<add name="MyConnStr1" connectionString="My connection string" />
</connectionStrings>
Usage is same as it was before:
var setting1 = ConfigurationManager.AppSettings["Setting1"];
var connString1 = ConfigurationManager.ConnectionStrings["MyConnStr1"].ConnectionString;
//Helps to open the Root level web.config file.
Configuration webConfigApp = WebConfigurationManager.OpenWebConfiguration("~");
//Modifying the AppKey from AppValue to AppValue1
webConfigApp.AppSettings.Settings["AppKey"].Value = "AppValue1";
<appSettings>
<add key="AppKey" value="AppValue"/>
</appSettings>
I am making a windows service.
It contain one App.config file as below:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="SQLConnectionStr" value="Data Source=192.168.1.116;Initial Catalog=Conezone;User Id=sa;Password=saadmin#123;"/>
<add key="FilePath" value="D:\Autoparts Guru\LINES2\"/>
</appSettings>
</configuration>
To get FilePath value in service code, I am writing
ConfigurationManager.AppSettings["FilePath"].ToString()
But it generates NullReferenceException.
Hope i get the answer quickly.
This code may help you:
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="SQLConnectionStr" value="Data Source=000.000.000.000;Initial Catalog=mydb;User Id=sa;Password=sa;"/>
<add key="FilePath" value="myfilepath"/>
</appSettings>
</configuration>
Namespace
using System.Configuration;
Code:
string filepath = string.Empty;
filepath = ConfigurationManager.AppSettings["FilePath"];
When I execute my app (Winform)from the computer where it was developed there is no error, but When I execute this in another computer I get the error. My App.config is like this:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="SecurityKey"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0"sku="...."/>
</startup>
</configuration>
and this is the line that I use:
string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
I already tried to follow this The key 'UserID' does not exist in the appSettings configuration section, but it is still the same.
Any suggestions?
the appSettings in the .config file is different from .settings file.
Take a look at ConfigurationManager.AppSettings Property.
I'd also mention that I have no idea how either the settingsReader nor the ConfigurationManager work with a key with no value:
<add key="SecurityKey"/> <!-- no value? -->
<add key="SecurityKeyWithValue" value="myvalue"/>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="SecurityKey" value="Syed Moshiur Murshed"/>
</appSettings>
</configuration>
I have a custom configSection that works as expected. However, when I add a 'connectionStrings' section, I receive error:
Configuration system failed to initialize
on line:
StencilObjects so = ConfigurationManager.GetSection( "stencilObjects" ) as StencilObjects;
Here is the config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="connection" connectionString="foo"/>
</connectionStrings>
<configSections>
<section name="stencilObjects" type="Stencil.Configuration.StencilObjects, Stencil.Configuration"/>
</configSections>
<stencilObjects>
<tableData>
<table schema="Auth" name="SecurityQuestion" />
</tableData>
</stencilObjects>
</configuration>
Is there any limitation when using a custom config section? Does this not allow the use of connectionstrings?
Again, when I remove the connectionStrings, the app runs as expected.
Any idea on what is going on?
I haven't found a link to back this up yet with an explicit statement, but I've always used configSections at the top of the file without any problems. Try like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="stencilObjects" type="Stencil.Configuration.StencilObjects, Stencil.Configuration"/>
</configSections>
<connectionStrings>
<add name="connection" connectionString="foo"/>
</connectionStrings>
<stencilObjects>
<tableData>
<table schema="Auth" name="SecurityQuestion" />
</tableData>
</stencilObjects>
</configuration>
configSections definitely doesn't need to be just before the section(s) it describes. connectionStrings can be in between.