我正在寻找一种方法来存储可以使用应用程序设置在运行时写入的应用程序或机器级别设置.用户设置允许读/写,但应用程序设置不允许.我一直在使用用户设置在运行时保存这样的设置,但由于以下原因,这已被证明是不切实际的:
我们公司的机器仅供一个用户使用,因此通常不需要用户特定的设置.
否则我真的很喜欢使用应用程序设置,并希望尽可能继续使用它们.如果设置可以与EXE位于同一个文件夹中(比如曾经做过的好的'ini文件),那将是理想的选择.
注意:这是一个WPF应用程序,而不是ASP.net Web应用程序,所以没有web.config.
我需要从业务类的appsettings.json文件(section :)中读取属性列表placeto,但我无法访问它们.我需要将这些属性公之于众.
我在Program课堂上添加文件:

这是我的appsettings.json:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"placeto": {
"login": "fsdfsdfsfddfdfdfdf",
"trankey": "sdfsdfsdfsdfsdf"
}
}
Run Code Online (Sandbox Code Playgroud) 如何使用Windsor容器将appSettings条目(从app.config或web.config)的值注入服务?如果我想将一个Windsor属性的值注入服务中,我会做这样的事情:
<properties>
<importantIntegerProperty>666</importantIntegerProperty>
</properties>
<component
id="myComponent"
service="MyApp.IService, MyApp"
type="MyApp.Service, MyApp"
>
<parameters>
<importantInteger>#{importantIntegerProperty}</importantInteger>
</parameters>
</component>
Run Code Online (Sandbox Code Playgroud)
但是,我真正想做的是#{importantIntegerProperty}从应用程序设置变量中获取值,该变量可能是这样定义的:
<appSettings>
<add key="importantInteger" value="666"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)
编辑:澄清; 我意识到,温莎和David Hayden的文章指出,滑盖式枪口指的是实际上是关于他自己的(David Hayden的)IoC容器,而不是温莎.
我肯定不是第一个遇到这个问题的人,所以我想知道的是其他人如何解决这个问题?
我做的很多是从AppSettings读取整数.最好的方法是什么?
而不是每次都这样做:
int page_size;
if (int.TryParse( ConfigurationManager.AppSettings["PAGE_SIZE"], out page_size){
}
Run Code Online (Sandbox Code Playgroud)
我在想我Helpers班上的一个方法是这样的:
int GetSettingInt(string key) {
int i;
return int.TryParse(ConfigurationManager.AppSettings[key], out i) ? i : -1;
}
Run Code Online (Sandbox Code Playgroud)
但这只是为了节省一些按键.
理想情况下,我喜欢将它们全部放入某种我可以使用intellisense的结构中,因此我不会遇到运行时错误,但我不知道我是如何处理这个...或者如果这是可能的话.
从Web.Config的AppSettings部分获取和读取整数的最佳实践方法是什么?
还有一件事...
设置它不是一个好主意readonly吗?
readonly int pageSize = Helpers.GetSettingInt("PAGE_SIZE") 似乎不起作用.
无法弄清楚如何读取startup.cs之外的appsettings.json值.我想做的是,例如,在_Layout.cshtml中,从配置中添加站点名称:
例如:
ViewData["SiteName"] = Configuration.GetValue<string>("SiteSettings:SiteName");
Run Code Online (Sandbox Code Playgroud)
甚至更好:
public class GlobalVars {
public static string SiteName => Configuration.GetValue<string>("SiteSettings:SiteName");
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我的代码:
[appsettings.json]
"SiteSettings": {
"SiteName": "MySiteName"
}
Run Code Online (Sandbox Code Playgroud)
[startup.cs]
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
var siteName = Configuration.GetValue<string>("SiteSettings:SiteName");
}
public IConfigurationRoot Configuration { get; }
Run Code Online (Sandbox Code Playgroud)
也许我正在阅读文档错误,但我似乎无法在Startup类之外公开Configuration对象.
目前在Startup中,我的sql server字符串如下所示:
public void ConfigureServices(IServiceCollection services)
{
var connection = @"Server=servername;Database=database;Trusted_Connection=True;MultipleActiveResultSets=true";
services.AddDbContext<CRAMSContext>(options => options.UseSqlServer(connection));
}
Run Code Online (Sandbox Code Playgroud)
我如何使用appsettings.json中的内容:
{
"Data": {
"DefaultConnection": {
"ConnectionString": "Data Source=server;Initial Catalog=database;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
Run Code Online (Sandbox Code Playgroud)
在新的ASP.NET 1.0 CORE新设置中看起来像这样看起来像这样参数化:
public void ConfigureServices(IServiceCollection services)
{
var connection2 = new SqlConnection connectionString;
services.AddDbContext<CRAMSContext>(options => options.UseSqlServer(connection2));
}
Run Code Online (Sandbox Code Playgroud)
另外,如果我有一个不同的测试数据库和qa,我如何知道ASP.NET应用程序为每个环境使用连接?
我的启动类已经在根目录中定义如下:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables(); …Run Code Online (Sandbox Code Playgroud) 默认情况下,设置存储在: C:\Documents and Settings\\Local Settings\Application Data\<Project Name>
如何将此路径更改为应用程序目录.我也不想为不同的用户提供不同的文件.如何使设置全局?
我试图将设置的范围更改为"应用程序",但我无法在运行时更改它们.
我正在编写一个可以使用几个不同主题的页面,我将在web.config中存储有关每个主题的一些信息.
是否更有效地创建一个新的sectionGroup并将所有内容存储在一起,或者只是将所有内容放在appSettings中?
configSection解决方案
<configSections>
<sectionGroup name="SchedulerPage">
<section name="Providers" type="System.Configuration.NameValueSectionHandler"/>
<section name="Themes" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<SchedulerPage>
<Themes>
<add key="PI" value="PISchedulerForm"/>
<add key="UB" value="UBSchedulerForm"/>
</Themes>
</SchedulerPage>
Run Code Online (Sandbox Code Playgroud)
要访问configSection中的值,我使用以下代码:
NameValueCollection themes = ConfigurationManager.GetSection("SchedulerPage/Themes") as NameValueCollection;
String SchedulerTheme = themes["UB"];
Run Code Online (Sandbox Code Playgroud)
appSettings解决方案
<appSettings>
<add key="PITheme" value="PISchedulerForm"/>
<add key="UBTheme" value="UBSchedulerForm"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)
要在appSettings中访问值,我正在使用此代码
String SchedulerTheme = ConfigurationManager.AppSettings["UBSchedulerForm"].ToString();
Run Code Online (Sandbox Code Playgroud) 我们目前正在计划一个更大的WPF LoB应用程序,我想知道其他人认为是存储大量UI设置的最佳实践,例如
我不喜欢使用提供的SettingsProvider(即App.config文件)拥有数十个存储值的想法,尽管它可以用于使用自定义SettingsProvider将其存储在嵌入式数据库中.能够使用某种数据绑定也是一个问题.有谁有同样的问题?
你做了什么来存储很多用户设置?
NLog.config文件未设置连接字符串.
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Warn"
internalLogFile="c:\temp\internal-nlog.txt">
<!-- Load the ASP.NET Core plugin -->
<extensions>
<add assembly="NLog.Web.AspNetCore" />
</extensions>
<variable name="SirNLogDb" value="data source=SQL_MULALLEY;initial catalog=LogFiles;User ID=xxx;Password=yyy;">
</variable>
<!-- providerName="System.Data.SqlClient"-->
<!-- the targets to write to -->
<targets>
<target name="db"
xsi:type="Database"
dbProvider="System.Data.SqlClient"
connectionString="${var:SirNLogDb}"
commandType="StoredProcedure"
commandText="[dbo].[NLog_AddEntry_p]">
<parameter name="@machineName" layout="${machinename}" />
<parameter name="@siteName" layout="${iis-site-name}" />
<parameter name="@logged" layout="${date}" />
<parameter name="@level" layout="${level}" />
<parameter name="@username" layout="${aspnet-user-identity}" />
<parameter name="@message" layout="${message}" />
<parameter name="@logger" layout="${logger}" />
<parameter name="@properties" layout="${all-event-properties:separator=|}" />
<parameter …Run Code Online (Sandbox Code Playgroud) appsettings ×10
c# ×6
asp.net-core ×2
web-config ×2
wpf ×2
.net ×1
app-config ×1
asp.net ×1
nlog ×1
performance ×1
settings ×1