I've added a reference to System.Configuration. I've created App1.config in my project and populated it with the following code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ResistanceA" value="0.04"/>
<add key="ResistanceB" value="0.04"/>
<add key="ResistanceC" value="0.01"/>
<add key="TempBattLow" value="40"/>
<add key="TempBattHigh" value="45"/>
<add key="TempLoad" value="40"/>
</appSettings>
</configuration>
Then I try to read the values using the following code,
using System.Configuration;
string str = ConfigurationManager.AppSettings.Get("ResistanceA");
However I do not get the data. Any idea what I am doing wrong? Thanks.
Make sure the (app name here).config file actually appears in the same folder as your (app name here).exe file. Being that you called it App1.config, I'm guessing that you have more than one.
Visual Studio renames App.Config to the actual (app name here).config file during a build, not App1.config.
Related
I have the following in my web.config located at the root of my project:
<configuration>
<connectionStrings>
<clear />
<add name="Default" providerName="System.Data.SqlClient" connectionString="Server=tcp:whoops;Encrypt=True;TrustServerCertificate=False;Connection Timeout=3000;" />
</connectionStrings>
<appSettings>
<add key="ConnectionString" value="test"/>
</appSettings>
....
I read from Startup.cs (this is an asp.net core web app):
string connection = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
However when I break on this, ConfigurationManager.ConnectionStrings and ConfigurationManager.AppSettings are empty (well, the first has some default connection string that is not the one in web.config).
What's going on here?
You will have to migrate the config to the new file appsettings.json
https://learn.microsoft.com/en-us/aspnet/core/migration/configuration?view=aspnetcore-2.1
Not saying this is how you should do it, but you can do the following...
In ASP.Net Core 2.2, you should be able to add an XML configuration to IConfigurationBuilder using
configBuilder.AddXmlFile("app.config");
Contents is pretty much the same as above...
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings configSource="connectionStrings.config" />
<appSettings>
<add key="Test" value="TestValue" />
</appSettings>
</configuration>
You should then be able to access AppSettings/ConnectionStrings using...
ConfigurationManager.ConnectionStrings
ConfigurationManager.AppSettings.AllKeys
{string[1]}
[0]: "Test"
ConfigurationManager.AppSettings.Get("Test")
"TestValue"
I don't know why but i don't have AppSettings in my app.config.file.
If i add it, when i run my apps, appSetting is deleted, so i'm unable to read my data !
Here is my file :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<add key="RepertoireEntree" value="E:\DEV\Autres\DevAgréga\Input\*.txt"/>
<add key="RepertoireSortie" value="E:\DEV\Autres\DevAgréga\Output\"/>
</configuration>
And in c# :
string RepertoireEntree = ConfigurationManager.AppSettings["RepertoireEntree"];
You have your app settings in a appSettings element, else it won't be recognized:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="RepertoireEntree" value="E:\DEV\Autres\DevAgréga\Input\*.txt"/>
<add key="RepertoireSortie" value="E:\DEV\Autres\DevAgréga\Output\"/>
</appSettings>
</configuration>
By the way, it is easier to use settings the usual way. You can do this by creating a settings file from your Project Settings > Settings tab.
Visual Studio will generate the XML elements in the app.config for you and you can reference your property like this:
string re = Properties.Settings.Default.RepertoireEntree;
You do not have your xml correct in the app.config file.
note the start and end tags for appSettings
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="RepertoireEntree" value="E:\DEV\Autres\DevAgréga\Input\*.txt"/>
<add key="RepertoireSortie" value="E:\DEV\Autres\DevAgréga\Output\"/>
</appSettings>
</configuration>
PS
While this is probably not an issue for you (but moreso for future readers)
Make sure you have this reference:
Namespace: System.Configuration
Assembly: System.Configuration (in System.Configuration.dll)
as noted on MSDN
Can someone please show me how to get winforms Webbrowsercontrol source from appconfig file ?.
I tried with following but it's not working
webBrowser2.Navigate = ConfigurationManager.AppSettings["dateandtime"];
and this is my how app config file look like
<?xml version="1.0"?>
<configuration>
<configSections>
</configSections>
<appSettings>
<add key="serverip" value="127.0.0.1" />
<add key="dbport" value="3306" />
<add key="defdatabase" value="waq115" />
<add key="dateandtime" value="http://free.timeanddate.com/clock/i4daxch9/n77/fs18/fcfff/tc212426/pc212426" />
</appSettings>
<startup><supportedRuntime version="v2.0.50727"/></startup>
</configuration>
Navigate() is a method not a variable, So the following line of code will not compile,
webBrowser2.Navigate = ConfigurationManager.AppSettings["dateandtime"];
You have to read the URL from the application configuration file and pass it as an argument to Navigate("URL") method as follows.
webBrowser1.Navigate(ConfigurationManager.AppSettings["dateandtime"]);
I have to add some constants to MVC 4 project.
Adding them to in web.config will work:
<appSettings>
<add key="MyConst1" value="123"/>
<add key="MyConst2" value="321"/>
<add key="MyConst3" value="234"/>
</appSettings>
Is there a way to create separate config file for this constants?
I guess you want to separate your appSettings from web.config, you can store them in a separate file and then specify that in your web.config under appSettings's configSource like:
<appSettings configSource="MySettings.config" />
and then you can have your settings in MySettings.config as:
<?xml version="1.0" encoding="UTF-8"?>
<appSettings>
<add key="MyConst1" value="123"/>
<add key="MyConst2" value="321"/>
<add key="MyConst3" value="234"/>
</appSettings>
You may see: Using configSource to split configuration files
If you put your code snippet in a file called appSettings.config, you can simply reference that file in your web config:
<appSettings configSource="appSettings.config" />
This is my first time using an XML config file. In the solution explorer I right click, add, new item, application config. file.
I then edited the file to read...
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Key1" value="1000" />
</appSettings>
</configuration>
...and tried to access it with...
Int32.Parse(ConfigurationManager.AppSettings.Get("Key1"));
...but it says that the parameter is null. I looked at just ConfigurationManager.AppSettings and it's AllKeys property has dimension 0.
What am I doing wrong?
Thanks!
Right-click on your project, choose Properties>Settings tab and edit your settings there because the config file is not the only place these values are stored at.
In your code use Settings.Default.MySetting to access the settings.
You'll need to add using MyProjectName.Properties; to your using statements in order to access the settings this way or you'll have to fully qualify it as MyProjectName.Properties.Settings.Default.MySetting...
I know this is super old but I just encountered this opening an old project. A reference to System.Configuration was missing.
To ensure get information from App.config using ConfigurationManager.AppSettings["<Key name>"]; be sure to set your values inside <appSettings> section.
Example:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<appSettings>
<add key ="IntervalSeconds" value ="60000"/>
<add key ="OutputPathLog" value ="\\myserver\hackingtrack\Projects\Log\"/>
<!--Jorgesys si Elenasys sunt puisori-->
<add key="InputFeeds1" value="https://cld.blahbla.com//portadaKick.json" />
<add key="InputFeeds2" value="https://cld.blahbla.com//portadaKick.json" />
<add key="InputFeeds3" value="https://cld.blahbla.com//portadaKick.json" />
</appSettings>
</configuration>
Retrieving values:
string intervalSeconds = ConfigurationManager.AppSettings["IntervalSeconds"];
string inputFeeds1 = ConfigurationManager.AppSettings["InputFeeds1"]; ;
string inputFeeds2 = ConfigurationManager.AppSettings["InputFeeds2"];
string inputFeeds3 = ConfigurationManager.AppSettings["InputFeeds3"];