添加和从配置文件中读取 [英] Adding and reading from a Config file

查看:121
本文介绍了添加和从配置文件中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 C#控制台基于项目。在该项目中我有一个像的companyName一些变量 companyType 这是字符串。

 的companyName =zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz;
companyType =ddddddddddddddddddddd;

我需要创建一个配置文件,并从中读取数值,然后初始化变量的companyName companyType 在code。


  1. 如何创建一个配置文件(或同等学历)?

  2. 如何从配置文件读取?


解决方案

  1. 添加应用程序配置文件项目到项目(右键单击项目>添加项目)。这将创建一个名为的app.config项目中的文件。


  2. 通过添加条目就像编辑文件<添加键=键名价值=someValue中/> <的appSettings方式> 标记


  3. 添加到 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.

  1. How can i create a config file (or equivalent) ?
  2. How can i read from the config file ?

解决方案

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

  2. Edit the file by adding entries like <add key="keyname" value="someValue" /> within the <appSettings> tag.

  3. Add a reference to the System.Configuration dll, and reference the items in the config using code like ConfigurationManager.AppSettings["keyname"]

这篇关于添加和从配置文件中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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