记录生活

windows服务和web环境怎样获取相对路径的配置文件

<Category: .NET> 查看评论
           string configFile = string.Empty;
            if (HostingEnvironment.IsHosted) //如果是web环境
            {
                configFile = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, ConfigurationManager.AppSettings["spring.config"]);
            }
            else
            {
	  string assemblyFilePath = Assembly.GetExecutingAssembly().Location;
	   string assemblyDirPath = Path.GetDirectoryName(assemblyFilePath);
                configFile = Path.Combine(assemblyDirPath, ConfigurationManager.AppSettings["spring.config"]);
            }
 
            if (File.Exists(configFile))
            {
                currentContext = new XmlApplicationContext(configFile);
            }
            else
            {
                throw new ConfigurationException("Spring 配置错误!"+configFile);
            }

相关文章 (Related posts)

本文来自: windows服务和web环境怎样获取相对路径的配置文件



发表评论