2012-11-06 30 views
6

是否可以使用F#脚本(fsx)文件中的app.config文件?如果是这样,怎么样?我在哪里放置文件,它会被称为什么?从fsx文件使用app.config

+3

这里已经回答:http://stackoverflow.com /问题/ 645622/APP-配置和-F-互动 - 不工作 –

回答

0

@JoeB提供的链接问题提供了2个更适合F#Interactive的答案,而不是.fsx脚本。我在下面提供F#脚本一个很好的解决方案:

鉴于这种app.config文件:

<configuration> 
    <appSettings> 
     <add key="foo" value="bar"/> 
    </appSettings> 
</configuration> 

阅读foo这样:

let appConfigPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "app.config") 
let fileMap = ExeConfigurationFileMap() 
fileMap.ExeConfigFilename <- appConfigPath 
let config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None) 
let foo = config.AppSettings.Settings.["foo"].Value 
Console.WriteLine(foo)