简体   繁体   English

将 app.config 添加到 .net core wpf 应用程序的正确方法是什么,因为它不像 .net 框架那样随附?

[英]What is the right way to add app.config to a .net core wpf application since it doesn't come with it unlike .net framework?

I like setting my connection string in my app.config and this has been very easy for me for a .net framework.我喜欢在我的 app.config 中设置我的连接字符串,这对我来说对于 .net 框架来说非常容易。 Since I migrated to .net core I noticed that it doesn't come with app.config.自从我迁移到 .net core 后,我注意到它没有附带 app.config。

In .NET Core and ASP.NET Core applications the application settings are stored in a JSON file named appsettings.json.在 .NET Core 和 ASP.NET Core 应用程序中,应用程序设置存储在名为 appsettings.json 的 JSON 文件中。 This is the default configuration for .NET Core projects.这是 .NET Core 项目的默认配置。 INI and XML files is also supported but JSON is the default.还支持 INI 和 XML 文件,但 JSON 是默认的。 You may also added to .Net Core WPF project appsettings.json config file, for this you need to install following nuget packegs:您还可以添加到 .Net Core WPF 项目 appsettings.json 配置文件,为此您需要安装以下 nuget packegs:

  Microsoft.Extensions.Configuration
  Microsoft.Extensions.Configuration.FileExtensions
  Microsoft.Extensions.Configuration.Json
  Microsoft.Extensions.DependencyInjection

after this,在这之后,

{
  "ConnectionStrings": {
    "myDataBaseConnectionString": "Server=myServer;Database=myDataBase;Trusted_Connection=True;"
  }
}

and you can read it in your code by calling the GetConnectionString method in the Microsoft.Extensions.Configuration namespace.您可以通过调用 Microsoft.Extensions.Configuration 命名空间中的 GetConnectionString 方法在代码中读取它。

var myDbConnectionString = _configuration.GetConnectionString("myDataBaseConnectionString");

Second way, you may use static class for keeping your database connection data:第二种方式,您可以使用静态类来保存您的数据库连接数据:

public static class dbConfig
    {
        public const string ConnectionString =
            "Data Source = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (COMMUNITY = TCP)(PROTOCOL = TCP)(HOST =192.100.10.11)(PORT = 1521)))(CONNECT_DATA = (SID = testdb))); User Id =admin; Password=admin;Pooling=true";
    }

and you may use from it any place of your assambly你可以在任何地方使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 相当于App.config转换.NET Core? - Equivalent to App.config transforms for .NET Core? 在 .Net Core 中使用 app.config - Using app.config in .Net Core 如何从 DBContext(实体框架核心)中的 App.config(不是 .net 核心应用程序)中读取值 - How to read value from App.config (not .net core app) in DBContext (which is entity framework core) 是否有直接方法将WPF .NET Framework应用程序转换为UWP .NET Core? - Is there a straightforward way to convert a WPF .NET Framework application to UWP .NET Core? 具有实体框架的WPF应用程序,app.config的正确设置 - WPF application with Entity Framework, proper settings for app.config 在Core 2.0 Web API项目中引用了.net Framework 4.x程序集的app.config迁移? - app.config migration for .net framework 4.x assemblies referenced in Core 2.0 Web API project? 如何在WPF应用程序中使用App.config进行log4net配置 - How to use App.config in WPF application for log4net configuration log4net compact framework 3.5没有app.config来添加文件追加器 - log4net compact framework 3.5 No app.config to add file appenders "无法使用 ConfigurationManager 在 C# .NET Core 单元测试项目中读取 app.config" - Can't read app.config in C# .NET Core unit test project with ConfigurationManager ASP.NET Core生成的app.config无效 - ASP.NET core generated app.config invalid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM