如何在Mac的.Net中使用appSettings [英] How to use appSettings in .Net for Mac

查看:34
本文介绍了如何在Mac的.Net中使用appSettings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Scenario: I installed Visual Studio on Mac and created a console application. Using Nuget I installed a library that needs some info from appSettings.

我没有任何app.config文件,或者我可以在其中添加XML的 appSettings

I do not have any app.config file or where I can add XML for appSettings

问题:我需要自己创建它还是如何创建?

Question: Do I need to create it myself or how does that work?

请原谅我的菜鸟问题,但我不知道该如何解决.

Please excuse my noob question but I have no idea how to get around this.

推荐答案

Visual Studio for Mac提供了一个app.config文件模板供您使用.

There is an app.config file template you can use that is provided by Visual Studio for Mac.

从文件"菜单中选择新建文件".然后在新文件对话框中选择杂项-应用程序配置文件".

From the File menu select New File. Then select Misc - Application Config File in the new file dialog.

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

Then add your appSettings to that file.

<?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 .

To avoid the warning about using System.Configuration.AppSettings, since this is deprecated, you will need to add a reference to System.Configuration by right clicking the References in the Solution window, selecting Edit References. Then search for System.Configuration in that dialog. Tick the reference and click OK. Then you can use System.Configuration.ConfigurationManager.AppSettings.

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

这篇关于如何在Mac的.Net中使用appSettings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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