标签: appsettings

如何在不使用用户设置的情况下在运行时读取/写入app.config设置?

我正在寻找一种方法来存储可以使用应用程序设置在运行时写入的应用程序或机器级别设置.用户设置允许读/写,但应用程序设置不允许.我一直在使用用户设置在运行时保存这样的设置,但由于以下原因,这已被证明是不切实际的:

  • 机器的所有用户都需要共享设置.
  • 在支持呼叫中(特别是在危机情况下),很难向用户/员工解释在何处手动查找和修改这些设置(appdata是隐藏文件夹等).
  • 新版本的应用程序需要使用以前的设置(用户设置似乎被新版本吹走了).
  • 我们的员工通常会将应用程序复制到新文件夹,该文件夹也会重置用户设置.

我们公司的机器仅供一个用户使用,因此通常不需要用户特定的设置.

否则我真的很喜欢使用应用程序设置,并希望尽可能继续使用它们.如果设置可以与EXE位于同一个文件夹中(比如曾经做过的好的'ini文件),那将是理想的选择.

注意:这是一个WPF应用程序,而不是ASP.net Web应用程序,所以没有web.config.

.net wpf app-config appsettings visual-studio

14
推荐指数
2
解决办法
3万
查看次数

从.NET Core 2中的类读取appsettings.json

我需要从业务类的appsettings.json文件(section :)中读取属性列表placeto,但我无法访问它们.我需要将这些属性公之于众.

我在Program课堂上添加文件:

课程

这是我的appsettings.json:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "placeto": {
    "login": "fsdfsdfsfddfdfdfdf",
    "trankey": "sdfsdfsdfsdfsdf"
  }
}
Run Code Online (Sandbox Code Playgroud)

c# appsettings asp.net-core-2.0

14
推荐指数
1
解决办法
2万
查看次数

使用Windsor注入应用程序设置

如何使用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容器,而不是温莎.

我肯定不是第一个遇到这个问题的人,所以我想知道的是其他人如何解决这个问题?

castle-windsor appsettings

13
推荐指数
1
解决办法
3967
查看次数

一遍又一遍地从AppSettings中读取整数

我做的很多是从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") 似乎不起作用.

web-config appsettings

13
推荐指数
1
解决办法
2万
查看次数

访问控制器类中的appsettings.json值

无法弄清楚如何读取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对象.

c# appsettings asp.net-core-mvc

13
推荐指数
1
解决办法
1万
查看次数

使用从appsettings.json到startup.cs的连接字符串

目前在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# appsettings asp.net-core asp.net-core-1.0

13
推荐指数
1
解决办法
2万
查看次数

如何更改.NET用户设置位置

默认情况下,设置存储在: C:\Documents and Settings\\Local Settings\Application Data\<Project Name>

如何将此路径更改为应用程序目录.我也不想为不同的用户提供不同的文件.如何使设置全局?

我试图将设置的范围更改为"应用程序",但我无法在运行时更改它们.

c# appsettings application-settings

12
推荐指数
1
解决办法
2万
查看次数

在web.config中存储值 - appSettings或configSection - 哪个更有效?

我正在编写一个可以使用几个不同主题的页面,我将在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)

asp.net performance web-config appsettings configsection

11
推荐指数
3
解决办法
2万
查看次数

存储UI设置的最佳做法?

我们目前正在计划一个更大的WPF LoB应用程序,我想知道其他人认为是存储大量UI设置的最佳实践,例如

  • 扩张国
  • 菜单订单
  • 调整属性
  • 等等...

我不喜欢使用提供的SettingsProvider(即App.config文件)拥有数十个存储值的想法,尽管它可以用于使用自定义SettingsProvider将其存储在嵌入式数据库中.能够使用某种数据绑定也是一个问题.有谁有同样的问题?

你做了什么来存储很多用户设置?

c# wpf settings appsettings

11
推荐指数
3
解决办法
5858
查看次数

无法在NLog中设置我的连接字符串

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)

c# nlog appsettings asp.net-core

11
推荐指数
1
解决办法
3291
查看次数