Visual Studio中的C#App.config嵌入式资源

Visual Studio中的C#App.config嵌入式资源,c#,app-config,C#,App Config,Visual Studio可以选择将App.config文件的生成操作作为“嵌入式资源”应用,这意味着在同一最终exe中包含App.config的内容。 好的 问题是:如何读取嵌入式App.config中的数据?例如,来自给定键的appSetting值 我以前从App.config读取的代码(物理上写在磁盘上的代码,通常名为program.exe.config)似乎不再工作了 string s = System.Configuration.ConfigurationManager.AppSett

Visual Studio可以选择将App.config文件的生成操作作为“嵌入式资源”应用,这意味着在同一最终exe中包含App.config的内容。 好的

问题是:如何读取嵌入式App.config中的数据?例如,来自给定键的appSetting值

我以前从App.config读取的代码(物理上写在磁盘上的代码,通常名为program.exe.config)似乎不再工作了

string s = System.Configuration.ConfigurationManager.AppSettings["mykey"];
可能它必须与其他专为这项工作设计的C#类进行重新调整


有什么想法吗?

您可以通过以下方法获得界面图标:

iconfigutibility.cs:

public interface IConfigUtility
{
    string LogFilePath
    {
        get;
    }

    string GetAppSetting(string key);
}
ConfigUtility.cs

using System;
using System.Configuration;

public class ConfigUtility : IConfigUtility
{
    Configuration config = null;
    public string LogFilePath
    {
        get
        {
            return GetAppSetting(@"Code to read the log file path");
        }
    }

    public ConfigUtility()
    {
        var exeConfigPath = this.GetType().Assembly.Location;
        try
        {
            config = ConfigurationManager.OpenExeConfiguration(exeConfigPath);
        }
        catch (Exception)
        {
        //handle error here.. means DLL has no satellite configuration file.
        }
    }

    public virtual string GetAppSetting(string key)
    {
        if (config != null)
        {
            KeyValueConfigurationElement element = config.AppSettings.Settings[key];
            if (element != null)
            {
                string value = element.Value;
                if (!string.IsNullOrEmpty(value))
                    return value;
            }
        }

        return string.Empty;
    }
}

现在,您可以使用上面的ConfigUtility.cs并从App.config文件中读取appsettings密钥

也许您应该看看这个:感谢您的快速回复。只有将构建操作设置为“无”时,这种方法才有效。如果我将其设置为“嵌入式资源”,则返回的值为空;看起来它看不懂。我应该重新调整此代码还是与配置相关?您可以重新调整此代码,我相信它也适用于您:)