控制台应用程序 C# 中的 App.Config 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10069254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:19:55  来源:igfitidea点击:

App.Config file in console application C#

c#console

提问by user1240679


I have a console application in which I want to write the name of a file.


我有一个控制台应用程序,我想在其中写入文件名。

Process.Start("blah.bat");

Normally, I would have something like that in windows application by writing the name of the file 'blah.bat'to Settingsfile in Properties.
However, here I didn't find any Settingsfile and I added an app.configfor the same purpose.

通常情况下,我会通过写文件的名称在Windows应用程序类似的东西'blah.bat'设置文件属性
但是,在这里我没有找到任何设置文件,我出于同样的目的添加了一个app.config

I am not sure what to write here in app.config, that would lead to me to achieve similar thing as in Windows Forms.

我不确定在app.config 中写什么,这会导致我实现与Windows Forms类似的事情。

For eg: In windows forms. Process.Start(Properties.Settings.Default.BatchFile);
where BatchFileis a string in settings file in Properties.

例如:在 windows 窗体中。属性中的设置文件中的字符串在Process.Start(Properties.Settings.Default.BatchFile);
哪里BatchFile

采纳答案by xxbbcc

You can add a reference to System.Configurationin your project and then:

您可以System.Configuration在项目中添加对 的引用,然后:

using System.Configuration;

using System.Configuration;

then

然后

string sValue = ConfigurationManager.AppSettings["BatchFile"];

string sValue = ConfigurationManager.AppSettings["BatchFile"];

with an app.configfile like this:

使用这样的app.config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <appSettings>
       <add key="BatchFile" value="blah.bat" />
   </appSettings>
</configuration>

回答by user5705992

use this

用这个

System.Configuration.ConfigurationSettings.AppSettings.Get("Keyname")

回答by Zin Min

For .NET Core, add System.Configuration.ConfigurationManager from NuGet manager.
And read appSettingfrom App.config

对于 .NET Core,从 NuGet 管理器添加 System.Configuration.ConfigurationManager。
并从App.config读取appSetting

<appSettings>
  <add key="appSetting1" value="1000" />
</appSettings>

Add System.Configuration.ConfigurationManager from NuGet Manager

从 NuGet 管理器添加 System.Configuration.ConfigurationManager

enter image description here

在此处输入图片说明

ConfigurationManager.AppSettings.Get("appSetting1")