GVKun编程网logo

如何以编程方式导致创build用户的configuration文件(创造编程器)

2

对于如何以编程方式导致创build用户的configuration文件感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解创造编程器,并且为您提供关于ASP.NETCore1.0Configur

对于如何以编程方式导致创build用户的configuration文件感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解创造编程器,并且为您提供关于ASP.NET Core 1.0 ConfigurationBuilder().AddJsonFile(“appsettings.json”);找不到文件、c# – ConfigurationManager.AppSettings不能用于以编程方式创建的App.config文件、c# – 如何以编程方式控制从T4生成的新文件的Build Action?、c# – 如何在Azure Function v2(核心)中静态使用ConfigurationBuilder?的宝贵知识。

本文目录一览:

如何以编程方式导致创build用户的configuration文件(创造编程器)

如何以编程方式导致创build用户的configuration文件(创造编程器)

如何以编程方式从WinServer 2008 r2的NetworkServices帐户创build用户的configuration文件。

THX,Alex

我如何提取当前用户的帐户图片?

如何检查计算机的域帐号是否无效(信任被破坏)?

机器不在活动目录中时如何获取本地机器组/用户列表?

如何在Windows上使用另一个用户帐户创build新的进程?

用户/密码问题与GetVolumeNameForVolumeMountPoint()(Windows远程驱动器挂载?)

我不知道什么是对NetworkServices帐户的限制,但要创建新用户,可以使用New-ADUser Cmdlet。

示例如何使用C#中的Cmdlet: “从C# 调用PowerShell Cmdlet”

总结

以上是小编为你收集整理的如何以编程方式导致创build用户的configuration文件全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

ASP.NET Core 1.0 ConfigurationBuilder().AddJsonFile(“appsettings.json”);找不到文件

ASP.NET Core 1.0 ConfigurationBuilder().AddJsonFile(“appsettings.json”);找不到文件

所以我终于找到了核心,我已经陷入了第一个障碍.我正在遵循Pluralsight ASP.NET核心基础课程,当尝试将appsettings.json文件添加到配置生成器时,我遇到异常.
public Startup()
{
    var builder = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json");

    Configuration = builder.Build();
}

我得到的错误是没有找到配置文件“appsettings.json”,不是可选的.但是我已经直接在我的解决方案下创建了,就像在课程视频中一样.

有什么建议么?

干杯,

@R_301_5609@

您需要添加以下包:
"Microsoft.Extensions.Configuration.Json": "1.0.0"

c# – ConfigurationManager.AppSettings不能用于以编程方式创建的App.config文件

c# – ConfigurationManager.AppSettings不能用于以编程方式创建的App.config文件

我遇到了一些有趣的事情.我正在尝试为我们的客户制作一个快速控制台应用程序来运行以更改某些程序上的某些连接字符串.这对客户来说是一个“快速应用程序”,我希望它是“白痴证明”.

该应用程序的主要功能之一是重新创建默认配置文件,如果找不到它.

if (!File.Exists("./App.config"))
        {
            Console.WriteLine("Config file is missing. Creating a new one Now..");
            //file doesn't exist,let's create one.
            var doc = new XDocument(
                new XDeclaration("1.0","UTF-16",null),new XElement("configuration",new XElement("startup",new XElement("supportedRuntime",new XAttribute("version","v4.0"),new XAttribute("sku",".NETFramework,Version=v4.5"))),new XElement("appSettings",new XElement("add",new XAttribute("key","targetDatabaseName"),new XAttribute("value","")),"applicationServer"),"sqlServer"),"applicationServerPort"),"customSearchPath"),"")))));


            using (var sw = new StringWriter())
            {
                using (var xw = XmlWriter.Create(sw))
                {
                    doc.Save(xw);
                    xw.Close();
                }

                doc.Save("./App.config");
                sw.Close();

            }

        }

在测试时,我注意到如果删除原始App.config文件并使用该应用程序重新创建,则所有ConfigurationManager.AppSettings行都将停止正常运行.即使我手动更改重新创建的配置文件中的值,它们也始终为每个键返回空值.

这让我觉得有一些基本的方式配置文件与他们的exe文件匹配?

如何让我的应用程序识别动态生成的app.config文件?

编辑

我添加了我用来从下面的配置文件中获取值的代码,以及我的应用程序创建的当前配置文件.

_targetDatabaseName = ConfigurationManager.AppSettings["targetDatabaseName"];
        _applicationServer = ConfigurationManager.AppSettings["applicationServer"];
        _sqlServer = ConfigurationManager.AppSettings["sqlServer"];
        _applicationServerPort = ConfigurationManager.AppSettings["applicationServerPort"];

        var emptyKeys = new List<string>();

        if (string.IsNullOrEmpty(_targetDatabaseName))
            emptyKeys.Add("targetDatabaseName");

        if(string.IsNullOrEmpty(_applicationServer))
            emptyKeys.Add("applicationServer");

        if(string.IsNullOrEmpty(_sqlServer))
            emptyKeys.Add("sqlServer");

        if(string.IsNullOrEmpty(_applicationServerPort))
            emptyKeys.Add("applicationServerPort");

        if (emptyKeys.Count > 0)
        {
            Console.WriteLine("You are missing one of the following required keys "
            + string.Join(",",emptyKeys) +". Press"
                + " a key to exit the application.");

            Console.ReadKey();
            Environment.Exit(0);
        }

这是当前配置文件以及实际值

<?xml version="1.0" encoding="utf-16"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>
    <add key="targetDatabaseName" value="" />
    <add key="applicationServer" value="" />
    <add key="sqlServer" value="" />
    <add key="applicationServerPort" value="" />
    <add key="customSearchPath" value="" />
  </appSettings>
</configuration>

解决方法

2件事会解决你的问题.

>配置文件需要是程序集的名称
>您需要告诉配置管理器从文件中重新加载您想要的部分而不是内存中的内容:

ConfigurationManager.RefreshSection( “的appSettings”)

有关配置管理器的信息,请访问MSDN:
https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager(v=vs.110).aspx

c# – 如何以编程方式控制从T4生成的新文件的Build Action?

c# – 如何以编程方式控制从T4生成的新文件的Build Action?

问题是:有没有办法在* .tt文件中设置一个设置,以便生成的文件设置为指定的Build Action?

问题是我使用模板生成代码,但它只是一个起点,消除了大量的输入.我不希望任何人按原样使用代码(生成的类),也不希望它混淆命名空间.目前,每次模板添加新文件时,我都必须手动将Build Action设置为None – 我想自动化它.

谢谢!

解决方法

一个非常简单的解决方法是将生成的代码包装到#if语句中:
#if GENERATED_CODE
    // my generated code
    // will compile only if the variable GENERATED_CODE is defined
#endif

c# – 如何在Azure Function v2(核心)中静态使用ConfigurationBuilder?

c# – 如何在Azure Function v2(核心)中静态使用ConfigurationBuilder?

将Azure功能从v1移植到v2时,配置管理器用于读取local.settings.json的方式发生了变化.

以前,我使用以下代码在函数实例之间启用redis连接池:

public static class Redis
{
    /// <summary>
    /// Initializes the REdis connection.
    /// </summary>
    private static readonly Lazy<ConnectionMultiplexer> LazyConnection = new Lazy<ConnectionMultiplexer>(() =>
    {
        return ConnectionMultiplexer.Connect(ConfigurationManager.AppSettings["CacheConnection"]);
        });

    public static IDatabase Database => LazyConnection.Value.GetDatabase();
}

但是在v2中,ConfigurationManager不再可用,我们必须使用以下内容:

new ConfigurationBuilder()
        .SetBasePath(context.FunctionAppDirectory)
        .AddJsonFile("local.settings.json",optional: true,reloadOnChange: true)
        .AddEnvironmentvariables()
        .Build();

但是,因为它需要仅在函数运行时期间可用的上下文,所以我们无法创建跨所有函数共享的静态类.是否可以在Azure Functions v2中静态读取app.settings.json?

解决方法

我们可以用

var config = new ConfigurationBuilder()
    .AddEnvironmentvariables()
    .Build();
string cacheConnection = config["CacheConnection"];

或者干脆

Environment.GetEnvironmentvariable("CacheConnection");

local.settings.json(还有Azure上的应用程序设置)中的值会在函数主机启动时自动注入到Environmentvariables中.

关于如何以编程方式导致创build用户的configuration文件创造编程器的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于ASP.NET Core 1.0 ConfigurationBuilder().AddJsonFile(“appsettings.json”);找不到文件、c# – ConfigurationManager.AppSettings不能用于以编程方式创建的App.config文件、c# – 如何以编程方式控制从T4生成的新文件的Build Action?、c# – 如何在Azure Function v2(核心)中静态使用ConfigurationBuilder?的相关知识,请在本站寻找。

本文标签: