Exemple #1
0
        /// <summary>
        /// 返回配置文件对象
        /// </summary>
        /// <param name="Porject">指定项目</param>
        /// <returns></returns>
        private static Configuration GetConfig(string Porject)
        {
            // 获取当前项目根路径
            string path = HttpContext.Current.Server.MapPath("~");

            // 若要跳转至其他项目路径
            if (!string.IsNullOrEmpty(Porject))
            {
                path = Utils.DelLastChar(path, "\\", 2) + "\\" + Porject + "\\";
            }

            // 判断是否存在项目路径
            if (!Directory.Exists(path))
            {
                return(null);
            }
            // 判断当前路径是否包含App.config或Web.config
            if (File.Exists(path + "App.config"))
            {
                path += "App.config";
            }
            else if (File.Exists(path + "Web.config"))
            {
                path += "Web.config";
            }
            else
            {
                return(null);
            }

            // 创建配置文件映射
            ExeConfigurationFileMap map = new ExeConfigurationFileMap();

            map.ExeConfigFilename = path;
            return(ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None));
        }