简体   繁体   English

C#:如何根据.csv中的列检查用户控制台输入?

[英]C#: How to check user console input against a column in a .csv?

Yo,

I am fairly new to C# and I'm making a small text based console RPG game just for practice and I was wondering if there was anyway I could make something similar to this: 我是C#的新手,我正在制作一个小型的基于文本的RPG游戏,仅供练习,我想知道是否仍然可以制作与此类似的东西:

  • Ask user for name 询问用户名称
  • Ask user for password 询问用户密码
  • Check if the name is in column 1 of a .csv file 检查名称是否在.csv文件的第1列中
  • If it is, check if the password is in column 2 and in the same row as the name 如果是,请检查密码是否在第2列中并且与名称位于同一行
  • If everything checks out, load the values from columns 3 and 4 in the same row as the name and password into corresponding values like health and level 如果一切顺利,请将与名称和密码相同的行中第3列和第4列的值加载到相应的值中,例如health和level

Here's an example .csv if I didn't really explain it well: 如果我没有很好地解释它,这是一个示例.csv:

"charactername", "password123", "health", "level"
"Oswald", "498562a", "100", "4"
"Hammerfist", "98457813", "77", "6"

So just for the sake of clarity, this is what I'm thinking: 因此,为了清楚起见,这就是我的想法:

  • Enter your username: Oswald 输入您的用户名:Oswald
  • Enter your password: 498562a 输入密码:498562a
    • Program reads through column 1 of the file looking for "Oswald" and finds a match 程序读取文件的第1列以查找“ Oswald”并找到匹配项
    • Program then checks to see if the password value is in the same row but in column 2 of the file and checks to see if it matches the user input, it matches it 然后程序检查密码值是否在同一行中但在文件的第2列中,并检查密码是否与用户输入匹配,是否与用户输入匹配
    • Program then loads the health and level values into character variables 然后程序将运行状况和级别值加载到字符变量中
  • Entrance Successful 入学成功

Is there a way to do this? 有没有办法做到这一点? Am I overlooking a simple solution? 我是否忽略了一个简单的解决方案? Or should I try and go about it a different way? 还是我应该尝试以其他方式去做? I'm just don't want to hard code the values into a Dictionary or List or Array or whatever. 我只是不想将值硬编码为Dictionary或List或Array或其他任何东西。 I'm not looking for security at this stage either, it's just for practice and fun. 我也不是在这个阶段寻找安全性,只是为了练习和娱乐。

Thanks in advance! 提前致谢! Let me know if you need a better understanding of what I'm trying to do and I'll try to explain the best I can. 如果您需要更好地了解我要做什么,请告诉我,我将尽力向您解释。

There are two ways to go about reading csv 阅读csv有两种方法

  1. Just like standard text file, line by line. 就像标准文本文件一样,逐行。 Then parse your line into array of columns and get your values 然后将您的行解析为列数组并获取值

  2. Using Microsoft.ACE.OLEDB.version 使用Microsoft.ACE.OLEDB.version

    OleDbconnection connection = new OleDbConnection(); OleDbconnection连接=新的OleDbConnection(); connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Documents\\file.csv;Extended Properties=\\"Text;HDR=Yes;FORMAT=Delimited\\""; connection.ConnectionString =“ Provider = Microsoft.ACE.OLEDB.12.0;数据源= C:\\ Documents \\ file.csv;扩展属性= \\” Text; HDR = Yes; FORMAT = Delimited \\“”;

This way you can load DataTable on the start of your app and query this table just like Sql database. 这样,您可以在应用程序的开头加载DataTable并查询该表,就像Sql数据库一样。

notice : HDR=Yes means that first row is a header row 注意:HDR = Yes表示第一行是标题行

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

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