Where has my Settings Editor gone in my Project Properties Menu?

后端 未结 3 350
误落风尘
误落风尘 2021-01-17 01:03

I\'m trying place a DB connection string in an app config file for my C# console project in VS Community 2019 so I can access the string from a central location. I had to cr

3条回答
  •  时光说笑
    2021-01-17 01:45

    It took all day looking through the forums for this one. I'm using .NET Core 3.0 in Visual Studio Community 2019. Basically, my ConfigurationManager was trying to access a config file from ~\bin\debug\netcoreapp3.0\DESCLogicFramework.dll.config. Where this file originated from I have no idea, but I assume it was just created automatically on building my app. Bottom line, I had to overwrite it.

    On a side note: I found this out using:

    var configfile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    

    and then exploring the configfile object in the debugger, and finding a property called "Filepath".

    The steps I took to correct it were:

    1. Rename my app.config file to be 'assemblyname'.exe.config, for example mine ended up being "DESCLogicFramework.exe.config"

    2. Place this config file in my root project directory.

    3. Change the config file Build Action Property to "Application definition" and its "Copy to Output Directory" Property to "Copy if newer". Both these can be accessed by right clicking on your config file and selecting "Properties". It will bring up the Properties window.

    4. Opening up the project's .csproj file, and adding the following lines of xml right before the closing ProjectTag, you tell the build to copy your .config file to the build location and give it the .dll.config extension.

      
      
        
      
      
    
    1. Afterwards, I made sure the project was "using System.Configuration" and accessed the file contents with
    var conn = ConfigurationManager.ConnectionStrings["DBconnection"];
    

    Edit: I am still not seeing the Settings Editor in my Project Properties, but I was able to solve the issue I was having without it.

提交回复
热议问题