从fsx文件使用app.config [英] Use app.config from fsx file

查看:70
本文介绍了从fsx文件使用app.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用F#脚本(fsx)文件中的app.config文件?如果是这样,怎么办?我应该在哪里放置文件以及将其命名为什么?

Is it possible to use an app.config file from a F# script (fsx) file? If so, how? Where do I put the file and what will it be called?

推荐答案

@JoeB链接的问题提供了2个答案,更多比.fsx脚本更适合F#Interactive。我为以下F#脚本提供了一个很好的解决方案:

The linked question by @JoeB provides 2 answers which are more suited to F# Interactive than .fsx scripts. I provide a good solution for F# scripts below:

给出此app.config文件:

Given this app.config file:

<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)

这篇关于从fsx文件使用app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆