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"]);
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 have a simple app.config file that doesn't quite pass the IDEs checker.
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="XmlRoot" type="System.String"/>
</configSections>
<connectionStrings>
<omitted/>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<XmlRoot>
<add key="relativepath" value=""/>
</XmlRoot>
</configuration>
I added the <section name="XmlRoot" type="System.String" /> part to the config and then I tried to define the name and key here: <add key="relativepath" value=""/>. But for some reason the IDE gives me the following Messages:
I rarely use the app.config file so it could just be a noob mistake. How do I make it recognize my tags?
If it's just a simple string, you can use AppSettings to accomplish this.
<configuration>
<appSettings>
<add key="relativepath" value="mypath" />
</appSettings>
</configuration>
Access it from C# like this:
string path = ConfigurationManager.AppSettings["relativepath"]);
Try changing your type to System.Configuration.NameValueSectionHandler
this is how I define my sections:
<configSections>
<sectionGroup name="MySettings">
<section name="serverConfiguration" type="System.Configuration.NameValueSectionHandler"></section>
<section name="sqlSettings" type="System.Configuration.NameValueSectionHandler"></section>
</sectionGroup>
</configSections>
...
<MySettings>
<sqlSettings>
<add key="sqlTransactionTimeOut" value="0" />
</sqlSettings>
<serverConfiguration>
<add key="resolveDnsAsync" value="true" />
</serverConfiguration>
</MySettings>
Probably you need to use the section appSettings
<configuration>
<appSettings>
<!-- and Here you define the key and the value like you do-->
<add key="relativepath" value="path"/>
</appSettings>
</configuration>
then in your code write something like this to read it.
String path = ConfigurationManager.AppSettings["relativepath"].ToString();
hope it works for you.
:)
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.
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"];