什么是配置数据?它与配置文件有什么关系?它与应用程序设置有什么关系?你什么时候使用它? [英] What is Configuration Data? How is it related to Configuration Files? How is it related to Application Settings? When do you use it?

查看:185
本文介绍了什么是配置数据?它与配置文件有什么关系?它与应用程序设置有什么关系?你什么时候使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始编写使用运行时提供的信息的程序。我遇到了一些用例,我希望我的应用程序从文件中读取文件而不是提示用户输入。

I'm new to writing programs that use information provided at runtime. I've come across use cases where I'd like my application to read from a file to know what to do instead of prompting for user input.

例如,需要一个C#应用程序来自特定目录的数据并对其进行处理(其中C#应用程序执行其他操作,例如持续监视该目录中的数据文件以进行更改,将数据插入数据库,或者对它们运行计算
- 实质上是该程序需要运行很长一段时间)。现在,该应用程序应该在给出包含数据文件的目录的任何系统路径的情况下执行其职责,并且假设路径不经常更改(例如每月一次,当用户希望程序执行其职责时
)另一个目录)。 

For example, a C# application that takes data from a specific directory and processes it (where the C# application does other things like continually monitors the data files in that directory for changes, inserts the data into a database, or runs computation against them -- in essence that program needs to run for a long period of time). Now, that application should perform its duties given any system path to the directory containing the data files and let's say the path doesn't change often (say once every month, when the user wants the program to perform its duties on a different directory). 

一个选项是让C#程序定期读取并解析包含要检查的目录的纯文本文件。纯文本文件可能包含如下所示的行:

One option is to have that C# program periodically read from and parse a plain-text file containing the directory to be examined. The plain-text file might contain a line that looks like this:

Data Directory: C:\Users\Alice\Data\TemperatureSensor Operation: RunningSum User Name: Alice

或者,C#程序可以定期查询数据目录检查,要完成的计算操作,以及用户名(阻止所有其他功能,如数据库插入等),直到用户介入提供
信息。这在我的用例中是不可取的。 
$

Alternatively, the C# program can periodically query for the Data Directory to examine, the computation operation to be done, and for the username (blocking all other functionality such as database insertions, etc...) until the user steps in to provide that information. This is undesirable in my use-case. 

是否有一个C#库来管理这些纯文本文件的程序访问,是否解析为我,并且一旦
用户更改了应用程序变量(例如,存储C#应用程序应该查找数据的字符串变量),就会更新它(因此纯文本文件是人类可读的,用户可以更改字段值以影响程序行为)? 



我相信这些纯文本文件称为配置文件,但我我对使用该术语犹豫不决,因为它可能意味着其他东西/在.NET框架中被称为其他东西。我应该查看System.Configuration API吗? 

Is there a C# library that manages program access of such plain-text files, does the parsing for me, and updates the application variables (e.g., the string variable that stores where the C# application should look for data) as soon as it is changed by the user (so the plain-text file is human-readable, the user can change field values to affect the program behavior)? 

I believe such plain-text files are called Configuration files but I'm hesitant to use that term since it could mean something else/be called something else in the .NET framework. Should I look at the System.Configuration API? 

推荐答案

"

是和否。 .NET支持读取文本文件。根据您的操作方式,您可以使用

File.ReadAllText

File.ReadAllLines
最简单的情况。请参阅此
MSDN
关于选项的文章。但是,一旦读取,您使用它完全是应用程序特定的,因此您必须自己决定如何继续并编写该代码。 

Yes and no. .NET has support for reading text files. Depending upon how you want to do it you can use File.ReadAllText or File.ReadAllLines in the simplest case. Refer to this MSDN article on the options. But what you do with it once read is completely app-specific so you'll have to decide how to proceed and write that code yourself. 

对于文件的解析,如果你使用像你发布的非标准格式,那么你将不得不自己解析它。解析可以是简单的也可以是复杂的,具体取决于您的格式。在您的具体示例中,您只需使用

String.Split
分割文件的每一行。例如,以下代码将读取您之前提到的文件。

For the parsing of a file, if you use a non-standard format like you've posted then you're going to have to parse it yourself. Parsing can be simple or complex, depending upon your format. In your very specific example you can simply use String.Split to split each line of the file. For example the following code would read the file you mentioned earlier.

var lines = File.ReadAllLines("MyFile.txt");
foreach (var line in lines)
{
   //Parse
   var tokens = String.Split(':');
   var key = tokens[0];
   var value = tokens[1];
};

在上面的示例中,您的应用需要决定如何处理键/值对。当然,这里也没有发生错误检查。由于你的示例文件正在分崩离析(我们通常认为是这样),你的app
的记录负责识别一条记录的结束而另一条记录的开始。

In the above example your app would then need to decide what to do with the key/value pair. Of course no error checking is happening here either. Since your example file is breaking up (what we'd normally consider to be) a record across multiple your app is responsible for identifying where one record ends and another begins.

如果您使用的是更标准的文件格式,如CSV或JSON,那么您可以使用库来阅读"记录"。容易。无需解析文件。这取决于你希望你的格式有多简单或复杂。

If you're using a more standard file format like CSV or JSON then there are libraries you can use to read the "records" easily. No file parsing is required. It depends upon how simple or complex you want your format to be.

" 我相信这样的纯文本文件被称为 配置
文件:"

否。
配置文件
in .NET通常是一个.config文件,它是一个基于XML的文件,由底层配置子系统管理,并通过

ConfigurationManager
。您不直接读取文件,因为.NET会处理它。它是一种复杂但灵活的格式。更重要的是,它实际上是为了存储在部署之外不会改变的应用程序级别的东西。在应用程序运行时更改
文件并不是开箱即用的。您必须执行一些额外的步骤,以便应用程序尝试写入其自己的配置文件非常罕见。 

No. A configuration file in .NET is generally a .config file that is an XML-based file that is managed by the underlying configuration subsystem and accessed via ConfigurationManager. You don't read the file directly because .NET handles it. It is a complex, but flexible, format. More importantly however it is really designed to store application-level stuff that doesn't change outside of deployment. Changing that file while the app is running isn't going to work out of the box. You have to go through some extra steps such that it is exceedingly rare that an app would try to write to its own config file. 

请注意,对于使用常规安装程序的标准应用程序那么您的应用程序实际上不会对其文件夹具有写入权限,因此您无法修改属于应用程序部署的任何应用程序,包括您可能尝试创建的App_Data文件夹。
要调整此项,您必须修改安装程序以正确设置安全性。对于大多数应用程序,如果您需要在应用程序运行时存储数据,则在数据需要跨用户共享并持久存储时,您将使用数据库。如果数据是按用户计算的话,则需要

可以使用每用户配置文件
或存储在用户的Documents目录中的任意数据文件。 

Note that for a standard application using a regular installer then your app won't actually have write permissions to its folder so you cannot modify any apps that are part of your application deployment, including the App_Data folder you may try to create. To adjust this you'd have to modify your installer to set up security properly. For most apps, if you need to store data while the app runs you would use a database if the data needs to be shared across users and persisted. If the data is per user then a per-user config file can be used or an arbitrary data file stored in the user's Documents directory. 

至于您的应用程序,它确实取决于它的应用程序类型以及数据变化的频率。如果您的应用程序按需运行或按计划运行,并且您想要调整其数据,那么您通常只使用命令行参数。这允许应用程序编写脚本。如果
你的应用程序更像是运行其他应用程序的驱动程序,那么文件是可行的但不灵活。至少你需要使用

FileSystemWatcher
监视文件的更改。这可能会变得非常复杂,具体取决于文件的编写方式,FSW本身可能有点令人沮丧,直到您了解它是如何工作的。但是,如果您的应用程序是一个长期运行的应用程序,并且该数据是来自其他应用程序的
,则数据库可能是更好的路径。它还取决于您完成数据后对数据执行的操作。您真的不希望多个应用同时更新同一个文件。问题将会发生。

As for your app it really depends what type of app it is and how often the data changes. If your app is run on demand or a schedule and you want to adjust its data then you'd normally just use command line arguments. This allows the app to be scripted. If your app is more of a driver that runs other apps then a file is doable but not flexible. At a minimum you'd need to use FileSystemWatcher to monitor the file for changes. This can get pretty complicated depending upon how the file is written and FSW itself can be a little frustrating until you understand how it works. But if your app is a long-running app and that data is coming from other apps then a database would probably be a better route. It also depends upon what you do with the data when you're done with it. You don't really want multiple apps updating the same file at the same time. Issues will occur.


这篇关于什么是配置数据?它与配置文件有什么关系?它与应用程序设置有什么关系?你什么时候使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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