asp.net 在自己指定的文件夹下面弄个App.config来读取配置

1.注意首先你要在你的应用程序的根目录下面新建一个Config的文件夹,然后在你新建的Config文件夹下面建立一个MyApp.config文件,根据标准的App.config复制一个过来稍作修改就好,然后右键改文件为始终复制,内容
2.这是我的MyApp.config的xml内容
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
  <appSettings>
    <add key="content" value="爱我所爱,hello world"/>
  </appSettings>
</configuration>
3. 程序调用


            ExeConfigurationFileMap exeConfigurationFileMap = new ExeConfigurationFileMap();
            exeConfigurationFileMap.ExeConfigFilename = AppDomain.CurrentDomain.BaseDirectory + "Config\MyApp.config" ;
            Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(exeConfigurationFileMap, ConfigurationUserLevel.None);
            string imip = configuration.AppSettings.Settings["content"].Value;
Console.WriteLine(imip);
4.结果为 爱我所爱,hello world
原文地址:https://www.cnblogs.com/kexb/p/6909581.html