简体   繁体   English

C#中应用程序的配置文件

[英]Configuration file for an application in C#

Could somebody walk me through the ins and outs of a configuration file: what is it, how to make it, what to do with it. 有人可以指导我完成配置文件的来龙去脉:它是什么,如何制作,如何使用它。

I was given an assignment by my senior to have a configuration file that will do the ff: remembering the file location of an xml file and logging it into the configuration file. 大四给我分配了一个配置文件,该文件将执行以下操作:记住xml文件的文件位置并将其记录到配置文件中。 For instance: upon opening the winforms, there's already a textbox with the file location written in it, when user open the filedialogbox and selects another file it would be written in the text box. 例如:打开winforms时,已经有一个文本框,其中写入了文件位置,当用户打开filedialogbox并选择另一个文件时,它将被写入文本框。 user closes the winform. 用户关闭winform。 next time the user opens it, the last selected file location would be in the textbox. 下次用户打开它时,最后选择的文件位置将在文本框中。

Is this possible? 这可能吗? My senior told me I could do this with the configuration file, as he is terribly busy, he cannot explain it to me further on what to do with it. 我的上级告诉我我可以使用配置文件来执行此操作,因为他非常忙,因此无法进一步向我解释如何使用该配置文件。 Based on his short explanation, am I correct to think that this config file is like an XML file? 根据他的简短解释,我是否正确地认为此配置文件就像XML文件一样? Any examples you could provide for easy understanding? 您可以提供任何示例以方便理解吗?

I saw this and this but I can't really understand much from it. 我看到了这个这个,但是我对此并不太了解。 Is there like a "Configuration Class (C#) for Dummies" out there? 有没有像“傻瓜配置类(C#)”在那里?

You can create your own class that contains all the user settings you want to save and then use XmlSerializer to read the file when the program first starts, and write when the program exits. 您可以创建自己的类,其中包含要保存的所有用户设置,然后使用XmlSerializer在程序首次启动时读取文件,并在程序退出时写入文件。

I gave up trying to use the built-in Settings . 我放弃了尝试使用内置Settings The main difficulty was trying to get the settings file to read from and write to the desired folder. 主要的困难是试图获取设置文件以读取和写入所需的文件夹。

Configuration file is the file which comes with the exe after compilation. 配置文件是编译后exe附带的文件。 In this file whatever values we are giving can be read from the application. 在此文件中,我们提供的任何值都可以从应用程序中读取。 So if there is any change in the values inside the application we can set those values in this configuration file. 因此,如果应用程序内部的值发生任何变化,我们可以在此配置文件中设置这些值。 For example, your application , the path of xml file is changing ,this can be set from configuration file First right click the project file from your solution explorer and select Add new item. 例如,您的应用程序xml文件的路径正在更改,可以从配置文件中进行设置。首先在解决方案资源管理器中右键单击项目文件,然后选择添加新项。 From the window select Applicatiopn COnfiguration file and give the the name app.config and press ok Inside the app.config . 从窗口中选择应用程序配置文件,并命名为app.config,然后在app.config中按ok。 inside the config section add 在配置部分中添加

<appSettings>
    <add key="Xmlpath" value="C:\temp\xmlfile.xml" />

 </appSettings>

Add System.Configuration to your references Add using System.Configuration; 将System.Configuration添加到您的引用中使用System.Configuration进行添加; to your form In page load 到页面加载中

string path = ConfigurationManager.AppSettings["Xmlpath"];
textbox.Text=path;

Even after compilation the xml path can be changed ,as app.config will is accessible from outside the exe. 即使在编译后,也可以更改xml路径,因为可以从exe外部访问app.config。

To change this path dynamically , you need to do more coding check this link 1. http://www.codeproject.com/Articles/14465/Specify-a-Configuration-File-at-Runtime-for-aC-Co 2. http://www.codeproject.com/Articles/14744/Read-Write-App-Config-File-with-NET like 要动态更改此路径,您需要执行更多编码,请检查此链接1. http://www.codeproject.com/Articles/14465/Specify-a-Configuration-File-at-Runtime-for-aC-Co 2。 http://www.codeproject.com/Articles/14744/Read-Write-App-Config-File-with-NET喜欢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM