IT Nota

  • Home
  • How To
  • .NET
  • WordPress
  • Contact

How to Use System.Configuration.ConfigurationManager on .NET CORE

When you need to convert an existing .NET Framework Console application to .NET Core, there’s one pending question that is kept being asked. Can you still use the System.Configuration and App.config (XML) configuration instead of using the new Appsettings.json file instead?

There’s always a need for a most robust configuration file and with JSON, it offers more flexibility and scalability but it doesn’t make sense to use it for a console application that only need to read one or two values as parameters from the *.config file.

As an example on this particular case, the console file just need a data of which file to look for based on a date.

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"  />
    </startup>
  <appSettings>
    <!-- Date value must be in mm/dd/yyyy format. -->
    <add key="Rundate" value="mm/dd/yyyy"  />
  </appSettings>
</configuration>

Running a .NET Core console application can expedite the runtime which will improve the completion time of the batch job and also as it’s leaner it will have a lower operating cost. Moreover, .NET Core is the future of .NET so we’re future proofing the processor.

So how do we use the same App.config file for a .NET Core application?

Setup the System.Configuration

  1. From your Visual Studio, open the Package Manager Console by going to Tools menu, then pick NuGet Package Manager. You will see the window open with the following initial message and PM> prompt.

      Each package is licensed to you by its owner. NuGet is not responsible for, nor does it grant any licenses to, third-party packages. Some packages may include dependencies which are governed by additional licenses. Follow the package source (feed) URL to determine any dependencies.
     
      Package Manager Console Host Version 4.9.2.5706
     
      Type 'get-help NuGet' to see all available NuGet commands.
     
      PM>
    

    Visual Studio NuGet Package Manager Console

  2. If you check the nuget.org website for System.Configuration.ConfigurationManager, you can just copy the command line (just substitute the version number you want to use).

      PM> Install-Package System.Configuration.ConfigurationManager -Version 4.5.0
    

    Or if you want to use the latest, just click on the Copy button and paste it on your Package Manager Console window.

    NuGet .NET System.Configuration.ConfigurationManager

  3. Once it’s installed, don’t forget to add using System.Configuration directive in your code.

    Add back System.Configuration directive in Visual Studio

  4. Open App.config file and remove the block so it will look like the following:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <!-- Date value must be in mm/dd/yyyy format. -->
        <add key="Rundate" value="mm/dd/yyyy"  />
      </appSettings>
    </configuration>
    
  5. Test the program.

This is just a simple way to convert your existing .NET Framework console application to .NET CORE without complicating the existing configuration.

Further Reading

.NET System.Configuration.ConfigurationManager
How to Check Installed .NET Core Version

February 7, 2019 Filed Under: How To Tagged With: .NET Core, Microsoft

How to Check Installed .NET Core Version

This is one way to determine what version of .NET Core is installed on your machine (or if it’s not installed):

  1. Launch Windows PowerShell.
  2. Runtime
    (dir (Get-Command dotnet).Path.Replace('dotnet.exe', 'shared\Microsoft.NETCore.App')).Name
    
  3. SDK
    (dir (Get-Command dotnet).Path.Replace('dotnet.exe', 'sdk')).Name
    

In the following example, you can see that .NET Core 2.1.2 Runtime is installed, but the SDK is not installed with the following error message:

dir : Cannot find path 'C:\Program Files\dotnet\sdk' because it does not exist.
At line:1 char:2
+ (dir (Get-Command dotnet).Path.Replace('dotnet.exe', 'sdk')).Name
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Program Files\dotnet\sdk:String) [Get-ChildItem], ItemNotFoundExcept
   ion
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
 
PS C:\Windows\system32>

PowerShell query for installed .NET Core version

Otherwise, it will look more like the next screenshot:

PowerShell query for installed .NET Core version for both Runtime and SDK

Another Way

There’s another way to accomplish the same thing with a much simpler command and this works on either Windows or macOS.

  1. Depending on your operating system, launch either Windows PowerShell or Terminal.
  2. Runtime
    dotnet --list-runtimes
    
  3. SDK
    dotnet --list-sdks
    
Installed .NET Core versions for both Runtime and SDK from PowerShell

Windows PowerShell

 

Installed .NET Core versions for both Runtime and SDK from macOS Terminal

macOS Terminal

 

When you see more than one entries, that means you have several versions installed and actually that’s the advantage of using .NET Core, you can have different versions installed side-by-side. The latest version is the last version number at the bottom of the result.

If you need a way to check the installed version of .NET Framework instead, check the link here.

Further Reading

How to Check Installed .NET Framework Version
How to determine if .NET Core is installed
How to: Determine which .NET Framework versions are installed
How to: Determine which .NET Framework security updates and hotfixes are installed
How to Remove .NET Runtime and SDK on Mac
How to Use System.Configuration.ConfigurationManager on .NET CORE

January 30, 2019 Filed Under: How To Tagged With: .NET, .NET Core, Microsoft, PowerShell, Windows

Buy me a coffee Support this site
Buy Me a Coffee?

Categories

  • .NET
  • Coding
  • Cybersecurity
  • Database
  • How To
  • Internet
  • Multimedia
  • Photography
  • Programming
  • Resources
  • Review
  • Tips and Tricks
  • Uncategorized
  • Use Case
  • WordPress
  • Writing

Recent Posts

  • How to View Stored Procedure Code in SQL Server
  • How to Find a String in SQL Server Stored Procedures
  • How to Remove Cached Credentials without Rebooting Windows
  • ESP Work Automation: Empowering Enterprises with Streamlined Workflows and Operational Efficiency
  • How to Search for a String in All Tables in a Database

Recent Posts

  • How to View Stored Procedure Code in SQL Server
  • How to Find a String in SQL Server Stored Procedures
  • How to Remove Cached Credentials without Rebooting Windows
  • ESP Work Automation: Empowering Enterprises with Streamlined Workflows and Operational Efficiency
  • How to Search for a String in All Tables in a Database

Tags

.NET .NET Core AdSense ASP.NET Cdonts Dll Classic ASP Code Editor ETL FSharp Genesis Framework Git Google HP Asset Manager HTML5 Hugo IIS Information Security Internet Internet Information Services iOS JAMStack Linux macOS Microsoft Microsoft SQL Server MVC PHP PowerShell Python Simple Mail Transfer Protocol Smtp Server SQL SQL Server SSIS SSMS SSRS Sublime Text Visual Studio Visual Studio Code VPN Windows Windows 8 Windows 10 Windows 2012 Windows Server

Copyright © 2011-2025 IT Nota. All rights reserved. Terms of Use | Privacy Policy | Disclosure