如何在.Net for Mac中使用appSettings

时间:2017-09-11 13:28:19

标签: c# .net nuget

场景:我在Mac上安装了Visual Studio并创建了一个控制台应用程序。使用Nuget我安装了一个需要appSettings的一些信息的库。

我没有任何app.config文件或我可以为appSettings

添加XML

问题:我是否需要自己创建它或者它是如何工作的?

请原谅我的noob问题,但我不知道如何解决这个问题。

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以使用由Visual Studio for Mac提供的app.config文件模板。

从“文件”菜单中选择“新建文件”。然后在新文件对话框中选择Misc - Application Config File。

然后将您的appSettings添加到该文件中。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <appSettings>
        <add key="Application Name" value="MyApplication" />
    </appSettings>
</configuration>

要避免使用System.Configuration.AppSettings的警告,因为不推荐使用此选项,您需要通过右键单击“解决方案”窗口中的“引用”,选择“编辑引用”来添加对System.Configuration的引用。然后在该对话框中搜索System.Configuration。勾选参考,然后单击“确定”。然后,您可以使用System.Configuration.ConfigurationManager.AppSettings

string setting = System.Configuration.ConfigurationManager.AppSettings["Application Name"];