最简单的有一个配置文件中的视窗方式窗体C#中的应用 [英] Simplest way to have a configuration file in a Windows Forms C# Application
问题描述
我真的很新的。NET,我仍然没有得到有关挂的配置文件是如何工作的。
I'm really new to .NET and I still didn't get the hang about how config files work.
我每次搜索谷歌这件事我得到的web.config结果,但我正在写一个Windows窗体应用程序。
Every time I search on google about it I get results about web.config, but I'm writing a windows forms application.
我想通了,我需要使用System.Configuration命名空间,但文件是没有帮助的。
I figured out that I need to use the System.Configuration namespace but the documentation isn't helping.
我如何定义我的配置文件是XYZ.xml?或者它有一个默认名称的配置文件?我还没有得到这一点。
How do I define that my config file is XYZ.xml? Or does it have a "default" name for the config file? I still didn't get that.
另外,如何定义一个新的部分,我真的需要创建一个类继承自ConfigurationSection?
Also, how do I define a new section, do I really need to create a class which inherits from ConfigurationSection?
我想只是有一些像这样的价值观的配置文件:
I would like to just have a config file with some values like this:
<MyCustomValue>1</MyCustomValue>
<MyCustomPath>C:\Some\Path\Here</MyCustomPath>
有没有一种简单的方法来做到这一点?你可以在一个简单的方式解释如何读写从/到一个简单的配置文件?
Is there a simple way to do it? Can you explain in a simple way how to read and write from/to a simple configuration file?
推荐答案
您想要使用一个app.config。
You want to use an App.Config.
在一个新的项目添加到项目中有一种叫做应用程序的配置文件。补充一点。
When you add a new item to a project there is something called Applications Configuration file. Add that.
然后你在配置/ AppSettings的部分添加键
Then you add keys in the configuration/appsettings section
像:
<configuration>
<appSettings>
<add key="MyKey" value="false"/>
访问通过执行成员
Access the members by doing
System.Configuration.ConfigurationSettings.AppSettings["MyKey"];
这工作在.NET 2及以上。
This works in .net 2 and above.
这篇关于最简单的有一个配置文件中的视窗方式窗体C#中的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!