CVS is a lightweight database, and text is separated by commas. In combination with generics, the method for reading this file is as follows:
Code
Public List < Dictionary < String , String > Getdata ( String Filepath, Out List < String > Column)
{
Column = New List < String > ();
List < Dictionary < String , String > Data = New List < Dictionary < String , String > ();
String Strline = "" ;
String [] Strarray;
Streamreader SR = New Streamreader (filepath );
Strline = Sr. Readline ();
If (Strline = Null | Strline = "" )
{
Return New List < Dictionary < String , String > ();
}
Strarray = Strline. Split ( ' , ' );
Foreach ( String Item In Strarray)
{
Column. Add (item );
}
Strline = Sr. Readline ();
While (Strline ! = Null )
{
Strarray = Strline. Split ( ' , ' );
Dictionary < String , String > Dict = New Dictionary < String , String > ();
For ( Int I = 0 ; I < Column. Count; I ++ )
{
Dict. Add (column [I], strarray [I]);
}
Data. Add (dict );
Strline = Sr. Readline ();
}
Sr. Close ();
Return Data;
}
The call method is as follows:
List <string> column;
List <dictionary <string, string> date = getdata (this. textbox1.text. Trim (), Out column );
String STR = "";
Foreach (Dictionary <string, string> item in date)
{
Foreach (string Item1 in column)
{
STR + = item [Item1] + "";
}
STR + = environment. newline;
}
This. textbox2.text = STR;