在C#中管理文本文件数据 [英] Managing Text File Data in C#
问题描述
我正在使用WinForms进行人员控制。每个人都有正确的用户名和三位数的代码(000到999之间的随机数),当用户将其代码输入文本框时,软件应查找匹配的用户名。
我将用户名和代码存储在一个简单的文本文件中:
ChrisHD22 123
ChrisCreateBoss 846
未知067
现在,如前所述,如果用户输入例如 846
,应用程序应自动显示输入的ChrisCreateBoss !
。问题是:我不知道如何管理两个数据值(用户名和代码),因此它们将匹配。
可能带有字符串[]用户
和一个字符串[]代码
,但我不知道如何将值分成每个字符串数组。我可以使用 List< string>用户
和一个代码也一样,但同样的事情,我无法整理数据。
在此先感谢 - CCB
I'm working in a personnel control with WinForms. Each person has its proper username and a code of three digits(a random number between 000 and 999), when the user enters its code to a textbox, the software should look for the matching username.
I'm storing the usernames and codes in a simple text file like this:
ChrisHD22 123
ChrisCreateBoss 846
Unknown 067
Now, as said before, if a user enters, for example, 846
, the application should automatically show ChrisCreateBoss entered!
. The problem is: I don't know how to manage the two data values(username and code) ,so they will match.
Maybe with a string[]Users
and a string[]Codes
, but I don't know how to divide the values into each string array. I could use a List<string> Users
and one for Codes too, but the same thing, I can't organize the data.
Thanks in Advance - CCB
推荐答案
假设您有每个用户名的唯一代码。
使用C#字典< TKey,TValue>
,并将代码作为键和用户名作为值添加到字典中。
您的字典看起来像:
Assuming, you have a unique code for each username.
Use C#Dictionary<TKey,TValue>
, and add you codes as keys and usernames as values into the dictionary.
Your dictionary will look something like:
Key Value
123 ChrisHD22
846 ChrisCreateBoss
067 Unknown
通过执行此操作,您可以搜索在文本框中输入的密钥并从字典中获取其对应的值。
检查以下链接,了解它的工作原理:
H ttps://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx [ ^ ]
http://www.dotnetperls.com/dictionary [ ^ ]
您可以使用与您需要的名称编号匹配的枚举器(枚举)。
ex,
Hi You can use enumerator (enum) which matches name-number which you require.
ex,
public enum usernames
{
ChrisHD22= 123,
ChrisCreateBoss= 846,
Unknown= 067
}
一些帮助链接:
HTTP:// stackoverflow.com/questions/309333/enum-string-name-from-value [ ^ ]
http://www.csharp-station.com/Tutorial/CSharp/lesson17 [ ^ ]
这篇关于在C#中管理文本文件数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!