oleg-shilo / cs-script.npp

CS-Script (C# Intellisense) plugin for Notepad++ (x86/x64)
MIT License
246 stars 52 forks source link

How do I connect App. config? #58

Closed it19862 closed 7 months ago

it19862 commented 3 years ago

Question.

  1. How do I connect App. config? 2.Did I connect App. config correctly? I 'm trying to connect the App.configuration. When I try to extract data from App. config, I connect null.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="countoffiles" value="7" />
    <add key="logfilelocation" value="abc.txt" />
  </appSettings>
</configuration>
using System.Configuration;
using System;
using System.Diagnostics;

namespace Folder_NS_02
{
    public class Test
    {  
        public static void Main(string[] args)
        {
            var con = ConfigurationManager.ConnectionStrings["App"];

            var path = ConfigurationManager.GetSection("countoffiles");

            string configvalue1 = ConfigurationManager.AppSettings["countoffiles"];
            string configvalue2 = ConfigurationManager.AppSettings["logfilelocation"];  

            Console.WriteLine(configvalue1);
            Console.WriteLine(configvalue2);

        }       
    }
}
oleg-shilo commented 3 years ago

Well, you indeed can use can run cs-script script with app config files. You can find the Config sample in the samples folder folder of legacy releases.

But I would strongly advise you to avoid using app.config as in the modern .net it is rather anti-pattern. you are better off using JSON config files.

And another reason is that the latest CS-Script is targeting .NET 5 (CS-Script NPP plugin will support it; planned to be done very soon). But .NET 5 has dropped support for app.config for dynamically loaded assemblies.