CSVファイルを読み込む
実行ファイルのフォルダの「CSV」フォルダ内のCSVファイルを読み込みます。
Imports System.IO Imports Microsoft.VisualBasic.FileIO Public Sub getCsvFile( fp As String) If File.Exists(fp) Then Using parser As New TextFieldParser(fp, System.Text.Encoding.GetEncoding("Shift_JIS")) parser.TextFieldType = FieldType.Delimited parser.SetDelimiters(",") Dim _row As String() While Not parser.EndOfData _row = parser.ReadFields() Dim _item As New clsItems _item.id = CDbl(_row(0)) _item.flag = CBool(_row(1)) _item.name = _row(2) _item.number = CInt(_row(3)) _items.Add(_item) End While End Using End If End Sub
呼び出し側
' アイテムクラス Public Class clsItems Public id As Double Public flag As Boolean Public name As String Public number As Integer End Class ' アイテムリスト Public _items As New List(Of clsItems) Dim fp As String = Path.Combine(Application.StartupPath, "csv") fp = Path.Combine(fp, "file1.csv") getCsvFile( fp )
このままでは、正常動作はしませんが、健忘録程度に記述します。