C#解析指定dll的app.config配置文件


传入指定的config配置文件路径,取该文件的配置信息

ConfigHelper configHelper = new ConfigHelper("D:\project\_garson\GZServiceTools\Debug\GZProxyServer.exe.config");
string ServiceName = configHelper.GetAppSettingsValue("ServiceName");
string DisplayName = configHelper.GetAppSettingsValue("DisplayName");
string Description = configHelper.GetAppSettingsValue("Description");

单独DLL中的app.config

C# 全选
string configFile = System.IO.Path.Combin(AppDomain.CurrentDomain.BaseDirectory, System.Reflection.Assembly.GetExecutingAssembly().GetName().Name+".dll.config");
ConfigHelper configHelper = new ConfigHelper(configFile);
string ServiceName = configHelper.GetAppSettingsValue("ServiceName");
string DisplayName = configHelper.GetAppSettingsValue("DisplayName");
string Description = configHelper.GetAppSettingsValue("Description");

GZProxyServer.exe.config内容

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="ServiceName" value="GZProxyServer"/>
    <add key="DisplayName" value="GZProxyServer在线代理服务"/>
    <add key="Description" value="支持HTTP代理"/>

    <add key="ListeningIPInterface" value="127.0.0.1"/>
    <add key="ListeningPort" value="8889"/>
    <add key="CertificateFile" value="cert.cer"/>
    
  </appSettings>
</configuration>

ConfigHelper.cs

C# 全选
public class ConfigHelper
{
    string _configfile;
    private string ConfigFile
    {
        get
        {
            if (String.IsNullOrEmpty(_configfile))
            {
                string assemblyFile = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
                _configfile = assemblyFile + ".config";
            }
            return _configfile;
        }
    }

    public ConfigHelper(string configFile)
    {
        _configfile = configFile;
    }

    /// <summary>  
    /// 获取配置文件的属性  
    /// </summary>  
    /// <param name="key"></param>  
    /// <returns></returns>  
    public string GetAppSettingsValue(string key)
    {
        string value = string.Empty;

        try
        {
            //if (File.Exists(ConfigFile))
            {
                XmlDocument xml = new XmlDocument();

                xml.Load(ConfigFile);

                XmlNode xNode = xml.SelectSingleNode("//appSettings");

                XmlElement element = (XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']");

                value = element.GetAttribute("value").ToString();
            }
        }
        catch { }

        return value;
    }


    /// <summary>  
    /// 获取配置文件的属性  
    /// </summary>  
    /// <param name="key"></param>  
    /// <returns></returns>  
    public string GetConnectionStringValue(string key)
    {
        string value = string.Empty;

        try
        {
            //if (File.Exists(ConfigFile))
            {
                XmlDocument xml = new XmlDocument();

                xml.Load(ConfigFile);

                XmlNode xNode = xml.SelectSingleNode("//connectionStrings");

                XmlElement element = (XmlElement)xNode.SelectSingleNode("//add[@name='" + key + "']");

                value = element.GetAttribute("connectionString").ToString();
            }
        }
        catch { }

        return value;
    }
}

 

版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
YES开发框架
上一篇:Windows服务程序开发
下一篇:windows Service 读取app.config AppSettings为空
评论列表

发表评论

评论内容
昵称:
关联文章

C#解析指定dllapp.config配置文件
C# 解析加载读取XML文件正确姿势
C# winform选择dll文件
C#操作 配置文件*.ini
windows Service 读取app.config AppSettings为空
python四种配置文件
.NET Core生成后没有Nugetdll文件
C# 配置文件增加自定义节点
.net Core编译出现错误:指定任务可执行文件“sgen.exe”未能运行,文件名或扩展名太长
未能加载文件或程序集“CefSharp.Core.dll”或它某一个依赖项。
LabVIEW生成.NETDLL——C#下调用NI数据采集设备功能一种方法 [原创www.cnblogs.com/helesheng]
Electron edge.js配置
.NET DLL反编译 JustDecompile
C#去除DLL强命名
Electron调用C#类库dll
C# 读取txt文件生成Word文档
C#根据文件流判断文件类型
.NET6中一些常用组件配置及使用记录,持续更新中。。。
C# web页面唤醒本地winform应用并执行指定命令
Visual Studio(VS)发布自动使用Net Reactor给生成DLL加壳

联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码