问题
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 create an app.config file by adding a new one to my project. When I try to access it using the ConfigurationManager, I get a NullReferenceException 'Object not set to an instance of an object'.
These are my App.config file contents:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name ="DBConnection"
providerName="System.Data.SqlClient"
connectionString = "Data Source=(localdb)\ProjectsV13; Initial Catalog=DATAMINE; Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" />
</connectionStrings>
<customSettings>
</customSettings>
</configuration>
And for the sake of simplicity, I'm attempting to access via:
using System;
using System.Configuration;
public class Program
{
static void Main(string[] args)
{
var sd = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
}
}
While troubleshooting I went into my project's Properties in order to access the Settings Editor, but the menu option isn't present (see pic). I even tried creating a new console application, checked its Properties and the Settings Editor doesn't exist there either. I appreciate any help!
Missing Settings Editor from Visual Studio Community 2019
回答1:
I had the same issue with the Properties/Settings.Settings.
- right click on Settings.Settings
- choose "Open with"
- Select "Settings Designer"
- click "set as default"
https://developercommunity.visualstudio.com/content/problem/962124/propertiessettings-editor-view-missing.html
回答2:
I test the code with your App.config, and it can get the connection string. Also, I recommend using appSettings
instead of customSettings
.
As to Settings
, it can be found in Visual Studio Enterprise 2019
.
回答3:
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:
Rename my app.config file to be 'assemblyname'.exe.config, for example mine ended up being "DESCLogicFramework.exe.config"
Place this config file in my root project directory.
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.
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.
<!-- START: This is a buildtime work around for https://github.com/dotnet/corefx/issues/22101 -->
<Target Name="CopyCustomContent" AfterTargets="AfterBuild">
<Copy SourceFiles="DescLogicFramework.exe.config" DestinationFiles="$(OutDir)\DescLogicFramework.dll.config" />
</Target>
<!-- END: This is a buildtime work around for https://github.com/dotnet/corefx/issues/22101 -->
- 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.
来源:https://stackoverflow.com/questions/61107752/where-has-my-settings-editor-gone-in-my-project-properties-menu