본문 바로가기
2/[ C# ]

[C#] csv load using StreamReader

by Kieran_Han 2022. 6. 3.
using ExcelDataReader;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Threading.Tasks;

namespace CSV_test
{
    class Program
    {
        static void Main()
        {
            // 경로 내 csv를 저장할 빈 list 생성
            List<string> listpath = new List<string>();

            // openFileDialog로 처음에 경로 지정 ↓예시
            string path = "C:\\Users\\KIERAN\\Desktop\\C# Test\\Data";

            if (Directory.Exists(path))
            {
                DirectoryInfo di = new DirectoryInfo(path);
                
                /// 지정한 경로 내 하위 폴더를 포함하여 csv 확장자만 불러옴
                var files = di.EnumerateFiles("*.csv", SearchOption.AllDirectories);

                /// listpath라는 리스트에 파일들 경로 하나씩 저장 ex) listpath[0]
                foreach (var item in files)
                {
                    listpath.Add(item.ToString());
                }
            }

            StreamReader sr = new StreamReader(listpath[0]);

            while (!sr.EndOfStream)
            {
                var aaaa = sr.ReadLine();

                var bbbb = aaaa.Split(',');

                Console.WriteLine(bbbb);
            }
        }
    }
}