Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

.net - Using C# to search a CSV file and pull the value in the column next to it

I am trying to make a program I am writing (C# in Visual Studio) look up a value in an external CSV file, and pull the value in the next column back in to a label in my WinForm.

My CSV file, which is a test with dummy data, is:

> old,newuser,newpassword
> firstlinetomakesure,firstnewusername,firstnewpassword
> adslusernameplaintext,thisisthenewuser,andthisisthenewpassword
> hello,terion,nadiomn
> somethingdownhere,thisisthelastuser,andthisisthelastpassword 
> 11,12,13
> 21,22,23 
> 31,32,33

I have tried using the solution at the link below, but could only return the value in the second column of the last line.

Search value in csv file using c#

I've been trying to make the program search the "old" column for a value, then pull in the value from the "newuser" column on the matching row. This would then go onto a label in the WinForm.

Any example code or suggestions would be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use a CSV library for reading CSV files. I like this one https://joshclose.github.io/CsvHelper/

Reading CSV files is not as straightforward as it seems. The number one difficulty is that values can have commas in them. The steps are simply, make a C# class to hold your data, map it to the data you see in your CSV file, and call the CSV library. That page I linked has enough examples to show you how to do this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...