Appsettings Get Value from .Config File

AppSettings get value from .config file

This works for me:

string value = System.Configuration.ConfigurationManager.AppSettings[key];

How to get the key value from the AppSettings.Config file?

The issue arise on renaming the App.Config file as AppSettings.Config. Thanks for all the guidances and help.

Reading settings from app.config or web.config in .NET

You'll need to add a reference to System.Configuration in your project's references folder.

You should definitely be using the ConfigurationManager over the obsolete ConfigurationSettings.

how to get value from appsettings.json

So there are really two ways to go about this.

Option 1 : Options Class

You have an appsettings.json file :

{
"myConfiguration": {
"myProperty": true
}
}

You create a Configuration POCO like so :

public class MyConfiguration
{
public bool MyProperty { get; set; }
}

In your startup.cs you have something in your ConfigureServices that registers the configuration :

public void ConfigureServices(IServiceCollection services)
{
services.Configure<MyConfiguration>(Configuration.GetSection("myConfiguration"));
}

Then in your controller/service you inject in the IOptions and it's useable.

public class ValuesController : Controller
{
private readonly MyConfiguration _myConfiguration;

public ValuesController(IOptions<MyConfiguration> myConfiguration)
{
_myConfiguration = myConfiguration.Value;
}
}

Personally I don't like using IOptions because I think it drags along some extra junk that I don't really want, but you can do cool things like hot swapping and stuff with it.

Option 2 : Configuration POCO

It's mostly the same but in your Configure Services method you instead bind to a singleton of your POCO.

public void ConfigureServices(IServiceCollection services)
{
//services.Configure<MyConfiguration>(Configuration.GetSection("myConfiguration"));
services.AddSingleton(Configuration.GetSection("myConfiguration").Get<MyConfiguration>());
}

And then you can just inject the POCO directly :

public class ValuesController : Controller
{
private readonly MyConfiguration _myConfiguration;

public ValuesController(MyConfiguration myConfiguration)
{
_myConfiguration = myConfiguration;
}
}

A little simplistic because you should probably use an interface to make unit testing a bit easier but you get the idea.

Mostly taken from here : http://dotnetcoretutorials.com/2016/12/26/custom-configuration-sections-asp-net-core/

how to display a value that is stored in appsettings of web.config file using javascript?

<appSettings>
<add key="insert" value="patient inserted successfully"/>
</appSettings>

In the web.config file inside the write this code
in the C# code file write the code for calling the JavaScript code

dispay()
{
hdn.Value = ConfigurationManager.AppSettings["insert"];
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text",
"msg()", true);
}

in the .aspx page write the JavaScript function to get the result

<script>
function msg()
{
alert('<%= hdn.Value%>');
}
</script>

this will display the message that is stored in appsettings of web.config file using the alert function of the JavaScript.

how to get value from appSettings for given config file using c# winform?

Finally I able to manage it , posting will help others

System.Configuration.ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = "C:\\mydemo\\web.config";

System.Configuration.Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
AppSettingsSection section = (AppSettingsSection)configuration.GetSection("appSettings");
if (section.Settings.AllKeys.Any(key => key == "MYDATA"))
{
section.Settings["MYDATA"].Value = updateConfigId;
configuration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}

How to get keys and values from Appsettings of Config file in Linq

It sounds like you actually want a dictionary of Key names and Value values instead of a string array.

var dict = 
ConfigurationManager.AppSettings.AllKeys
.ToDictionary(k => k, v => ConfigurationManager.AppSettings[v]);

Write and read updated appSettings from config file during runtime

You should try

ConfigurationManager.RefreshSection("appSettings");

Found an old post here
Reloading configuration without restarting application using ConfigurationManager.RefreshSection

unable to retrieve appsettings value

Your code is not wrong. And since ConfigurationManager.AppSettings("some-key") always returns a string for an existing property, you never need the .ToString() call.

The fact that it yields Nothing means it's not there. Since the keys do seem to match, most likely your app.config is not in the correct location.

Are you sure it's in the project that generates your .exe?

Edit:

Honestly there's no visible problem. I'd have to see your project's .vbproj xml to know for sure. I even double checked by creating a new empty VB project and copy-pasted your app.config over mine.

This is the whole code of the project:

Module Module1
Sub Main()
Dim culture = Configuration.ConfigurationManager.AppSettings("culture")
Console.WriteLine(culture)
Console.ReadKey()
End Sub
End Module

It nagged about the globalization section being unknown so I removed that section and left the rest as-is.

Console app output as expected: cs-CZ

You could try removing that section and see what happens but I doubt that's the culprit. Your app wouldn't run otherwise.

So on to the .vbproj XML:

There's a specific way in which Visual Studio adds configurations. It typically doesn't work out-of-the-box when you manually add these kind of files through simple copy-paste.

Is this the app.config that was freshly added to the project when you created it, or is this a manual add / copy-paste?

You could verify this:

  1. Right-click your project. press Unload Project

  2. Right-click your project again, press Edit [projectname].vbproj. You're now looking at the XML file that defines the project structure/settings etc.

  3. Find the piece <None Include="App.config" /> and confirm that it sits under an ItemGroup node together with two others, it should look roughly like this:

     <ItemGroup>
    <None Include="My Project\Application.myapp">
    <Generator>MyApplicationCodeGenerator</Generator>
    <LastGenOutput>Application.Designer.vb</LastGenOutput>
    </None>
    <None Include="My Project\Settings.settings">
    <Generator>SettingsSingleFileGenerator</Generator>
    <CustomToolNamespace>My</CustomToolNamespace>
    <LastGenOutput>Settings.Designer.vb</LastGenOutput>
    </None>
    <None Include="App.config" />
    </ItemGroup>

Again, I wouldn't expect this to be the issue because you said the application.exe.config is correctly generated to the output. It doesn't hurt to eliminate the possibilty though.

Lastly, a scoping issue:

.NET's configuration system is multi-inheritance starting at MACHINE.config and going all the way down to your app.config (with one or two layers of web/user context in between depending on the situation).

I don't know details beyond what you showed me, so technically it's possible that some settings on your computer / user profile / visual studio etc, make it so that the configuration hierarchy is loaded differently. Perhaps you're just getting a higher level config served.

You can "force" this with some slightly convoluted code. I wouldn't call it a solution, but at least it narrows down the issue if this actually works:

Imports System.Configuration
Module Module1
Sub Main()
Dim config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Dim culture = config.AppSettings.Settings("culture").Value 'Note you do need the .Value here; this is a different kind of config object
Console.WriteLine(culture)
Console.ReadKey()
End Sub
End Module


Related Topics



Leave a reply



Submit