• 00
  • :
  • 00
  • :
  • 00

Web应用程序dll获取当前程序集路径问题

public string ConfigFile
        {
            get
            {
                if (String.IsNullOrEmpty(_configfile))
                {
                    //string assemblyFile = System.Reflection.Assembly.GetExecutingAssembly().Location;
                    string assemblyFile = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
                    _configfile = assemblyFile + ".config";
                }
                return _configfile;
            }
        }

 

 

获得config配置信息:

/// <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;
}

 

posted @ 2021-02-18 22:07  Garson_Zhang  阅读(107)  评论(0编辑  收藏  举报