DataSet을 만들고 DataTable을 생성하여
해당 디렉토리에 있는 파일 데이터( 이름, 형식, 수정날짜, 크기 )를 넣고
DataGridView에 출력해보겠습니다.
DataSet ds = new DataSet(); |
private void btnSearch_Click_1(object sender, EventArgs e) { bool bCheckIsTalbe = false; FolderBrowserDialog fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() == DialogResult.OK) { string[] files = { "", }; try { files = Directory.GetFiles(fbd.SelectedPath, "*.*", SearchOption.AllDirectories); foreach (string f in files) { // DataSet안에 해당하는 DataTable이 있는지 확인 한다 if (ds.Tables.Contains("FileSearch")) { bCheckIsTalbe = true; } DataTable dt = null; if (!bCheckIsTalbe) // 기존 Table이 없을 경우 신규로 생성 { dt = new DataTable("FileSearch"); //DataColumn 생성 DataColumn colName = new DataColumn("이름", typeof(string)); DataColumn colType = new DataColumn("확장자", typeof(string)); DataColumn colDate = new DataColumn("수정한 날짜", typeof(string)); DataColumn colSize = new DataColumn("크기", typeof(string)); //생성된 Column을 DataTable에 Add dt.Columns.Add(colName); dt.Columns.Add(colType); dt.Columns.Add(colDate); dt.Columns.Add(colSize); } else // 기존 Table이 있을 경우 기존 Table을 가져온다 { dt = ds.Tables["FileSearch"]; } FileInfo info = new FileInfo(files[cnt]); //이름, 확장명, 수정한 날짜, 크기 string strFileName = info.Name; int lastIndex = strFileName.LastIndexOf('.'); // 이름만 출력될 수 있도록 뒤에 .부터 자른다. string strResult = $"{strFileName.Substring(0, lastIndex)}"; //가공된 이름 string strFileType = info.Extension; // 확장자명 string strFileDate = info.LastWriteTime.ToString(); // 수정한 날짜 string strFileSize = GetFileSize(info.Length); // 크기 if (files.IsFixedSize) { cnt++; } //Row 생성 DataRow row = dt.NewRow(); row["이름"] = strResult; row["확장자"] = strFileType; row["수정한 날짜"] = strFileDate; row["크기"] = strFileSize; //생성된 Row을 DataTable에 Add if (bCheckIsTalbe) { // DataSet에 해당 DataTalbe이 있을 경우 기존 Table에 Row를 추가한다 ds.Tables["FileSearch"].Rows.Add(row); } else { dt.Rows.Add(row); // 신규로 작성한 DataTable에 Row를 등록 하고, ds.Tables.Add(dt); // 등록한 DataTable을 DataSet에 추가한다 } } //DataGridView에 내용출력 dataGridView1.DataSource = ds.Tables["FileSearch"]; //DataGridView 컬럼 속성 설정 dataGridView1.Columns["수정한 날짜"].Width = 140; } catch (IOException ex) { MessageBox.Show(ex.Message); } cnt = 0; } } |
보통 DataSet을 활용할때 텍스트파일, Excel 또는 DB를 이용해서
데이터를 저장하고 불러오는 방법을 많이 쓰지만
단순히 해당 폴더에 있는 파일데이터를 불러오는것이 목적이기때문에
따로 텍스트파일이나 excel, DB에는 저장하지 않겠습니다.
모두 메모리상에서만 이루어지는 것이기때문에
프로그램이 종료되면 모두 지워집니다.
'Programing > C#' 카테고리의 다른 글
C# LOG파일 생성 (0) | 2022.03.12 |
---|---|
C# CrossThread Error 해결 (0) | 2022.02.21 |
C# Panel에 간단하게 타원(Rectangle)그리기 (0) | 2019.12.17 |
[DevExpress] SimpleButton 색변경 (0) | 2018.10.04 |
[DevExpress] XtraMessageBox DialogResult Settings (0) | 2018.10.04 |