I’d like to show file root/parent in the DataGridView, but I don’t know how. I listed all files that are in subdirectories in one root folder, but I’d also like that every file has its parent folder shown in DataGridView. Is there anyway to get this information from System.IO library?
This is my code:
private void Form1_Load(object sender, EventArgs e)
{
label2.Text = LocationX;
s1 = Directory.GetFiles(@LocationX, "#", SearchOption.AllDirectories);
for (int i = 0; i <= s1.Length - 1; i++)
{
if (i == 0)
{
dt.Columns.Add("Paren Folder Name");
dt.Columns.Add("File_Name");
dt.Columns.Add("Version");
dt.Columns.Add("File_Type");
dt.Columns.Add("File_Size");
dt.Columns.Add("Create_Date");
}
FileInfo f = new FileInfo(s1[i]);
FileSystemInfo f1 = new FileInfo(s1[i]);
dr = dt.NewRow();
dr["Root"] = f1.???????? //is it possible to do show parent folder this way?
dr["File_Name"] = f1.Name;
dr["File_Type"] = f1.Extension;
dr["File_Size"] = (f.Length / 1024).ToString();
dr["Create_Date"] = f1.CreationTime.Date.ToString("dd/MM/yyyy");
dt.Rows.Add(dr);
}
if (dt.Rows.Count > 0)
{
dataGridView1.DataSource = dt;
}
}
Use
FileInfo.Directoryto get directory name of file:Also move columns creation out of the loop. I think loop should be used only for adding data to table.