How to read file information from directory and write in DataGridView using Windows application?

In this article I have explained about how to read all file information like file name, size and created date from directory and write in the data grid view. This example is created in .Net windows application. The result is shows file size in KB.

In most of situation we need to read all files details in the particular directory. So I write below code for read specified directory files and write that file name, file size and created date. I am using windows application for write file details in datagridview.

Code Details:

Client Side:
Place one datagridview control in the design side.

Server side
Write below code in the Server side

using System.Data;
using System.IO;
namespace WindowsFormsApplication14
public partial class Form1 : Form
DataTable dt = new DataTable();
DataRow dr;
String[] s1;
public Form1()
private void Form1_Load(object sender, EventArgs e)
//Initialize Directory path
s1 = Directory.GetFiles(@"D:\test");
//File Name, File Type, File size, create date
for (int i = 0; i <= s1.Length - 1; i++)
if (i == 0)
//Add Data Grid Columns with name
//Get each file information
FileInfo f = new FileInfo(s1[i]);
FileSystemInfo f1 = new FileInfo(s1[i]);
dr = dt.NewRow();
//Get File name of each file name
dr["File_Name"] = f1.Name;
//Get File Type/Extension of each file
dr["File_Type"] = f1.Extension;
//Get File Size of each file in KB format
dr["File_Size"] = (f.Length / 1024).ToString();
//Get file Create Date and Time
dr["Create_Date"] = f1.CreationTime.Date.ToString("dd/MM/yyyy");
//Insert collected file details in Datatable
if (dt.Rows.Count > 0)
//Finally Add DataTable into DataGridView
dataGridView1.DataSource = dt;

Output is show look like this
I hope my article help to read file information from specified directory.


Guest Author: sachin@gmail.com09 Nov 2012

good one

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: