C# 将不带app.config文件的硬编码配置添加到某些程序集

C# 将不带app.config文件的硬编码配置添加到某些程序集,c#,.net,configuration,configuration-files,C#,.net,Configuration,Configuration Files,我需要向程序集本身添加配置信息,而不包括其app.config文件。 我怎么做 编辑: 我需要这样的东西 string config = @"<?xml version='1.0' encoding='utf-8'?> <configuration> . . . .

我需要向程序集本身添加配置信息,而不包括其app.config文件。 我怎么做

编辑: 我需要这样的东西

 string config = @"<?xml version='1.0' encoding='utf-8'?>
                   <configuration> 
                    .
                    .
                    .
                    .
                   </configuration>";
string config=@”
.
.
.
.
";

将此硬编码字符串配置设置为当前程序集配置

您可以拥有一个自定义XML文件,在其中存储设置,然后将其设置为嵌入式资源,以便在exe或dll中可用

请参见此处:有关如何在运行时读取它的示例


编辑:并将其作为自定义配置文件加载,请检查此处:

配置设置(无论是用户设置还是应用程序设置)默认情况下在程序集中具有其默认值“硬编码”。可以通过包括app.config,或在运行时修改用户设置并保存到用户配置文件来覆盖它们

创建项目设置(在项目属性中并转到“设置”选项卡)后,将使用静态属性生成一个
settings
类,该类将具有您配置的默认值

它们可以在整个部件中访问,如下所示:

Assert.AreEqual(Properties.Settings.MySetting, "MyDefaultValue");
可以通过app.config覆盖这些默认值:

<applicationSettings>
    <MyProject.Properties.Settings>
        <setting name="MySetting" serializeAs="String">
            <value>MyDefaultValue</value>
        </setting>
    </MyProject.Properties.Settings>
</applicationSettings>

是否要在程序集中嵌入配置?这就是所谓的常量:-)是的,这就是我想要的。assembly标记用于低级编程,而不是.NET程序集。如何将其设置为程序集的配置文件?有没有办法一次性刷新整个部分?!遗憾的是,我没有找到。不过,您可以使用LINQtoXML来遍历配置文件
// 1. Create a temporary file
string fileName = Path.GetTempFileName();
// 2. Write the contents of your app.config to that file
File.WriteAllText(fileName, Properties.Settings.Default.DefaultConfiguration);
// 3. Set the default configuration file for this application to that file
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", fileName);
// 4. Refresh the sections you wish to reload
ConfigurationManager.RefreshSection("AppSettings");
ConfigurationManager.RefreshSection("connectionStrings");
// ...