2012-11-06 32 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)