添加和从配置文件中读取 [英] Adding and reading from a Config file
问题描述
我创建了一个 C#控制台基于项目
。在该项目中我有一个像的companyName一些变量
, companyType
这是字符串。
的companyName =zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz;
companyType =ddddddddddddddddddddd;
我需要创建一个配置文件,并从中读取数值,然后初始化变量的companyName
, companyType
在code。
- 如何创建一个配置文件(或同等学历)?
- 如何从配置文件读取?
-
添加
应用程序配置文件
项目到项目(右键单击项目>添加项目)。这将创建一个名为的app.config项目中的文件。 -
通过添加条目就像编辑文件
<添加键=键名价值=someValue中/>
在<的appSettings方式>
标记 -
添加到
System.Configuration
DLL的引用,并使用code像ConfigurationManager中引用的配置项目。的AppSettings [键名]
I have created a C# console based project
. In that project i have some variables like companyName
, companyType
which are Strings.
companyName="zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
companyType="ddddddddddddddddddddd";
I need to create a config file and read values from it, and then initialize the variables companyName
, companyType
in the code.
- How can i create a config file (or equivalent) ?
- How can i read from the config file ?
Add an
Application Configuration File
item to your project (right click project > add item). This will create a file called app.config in your project.Edit the file by adding entries like
<add key="keyname" value="someValue" />
within the<appSettings>
tag.Add a reference to the
System.Configuration
dll, and reference the items in the config using code likeConfigurationManager.AppSettings["keyname"]
这篇关于添加和从配置文件中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!