How can I read a list of files from a folder into a DataTable?

  • Thread starter Thread starter Sasha
  • Start date Start date
S

Sasha

Hi,

What is the easiest way to populate a DataTable with a list of files
from a folder?

Thank you,
Sasha
 
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("FileName", typeof(System.String));
dt.Columns.Add(dc);
foreach(FileInfo fi in Directory.GetDirectories(@"C:\")){
DataRow dro = dt.NewRow();
dro[0] = fi.FileName;
dt.Rows.Add(dro);

}
 
Even shorter one ;-)
Instead of
DataRow dro = dt.NewRow();
dro[0] = fi.FileName;
dt.Rows.Add(dro);

do

dt.Rows.Add(new object[]{fi.FileName});

:-)

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
SLODUG - Slovene Developer Users Group
www.rthand.com

W.G. Ryan eMVP said:
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("FileName", typeof(System.String));
dt.Columns.Add(dc);
foreach(FileInfo fi in Directory.GetDirectories(@"C:\")){
DataRow dro = dt.NewRow();
dro[0] = fi.FileName;
dt.Rows.Add(dro);

}

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
Sasha said:
Hi,

What is the easiest way to populate a DataTable with a list of files
from a folder?

Thank you,
Sasha
 
Nice Catch Miha ;-)

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
Miha Markic said:
Even shorter one ;-)
Instead of
DataRow dro = dt.NewRow();
dro[0] = fi.FileName;
dt.Rows.Add(dro);

do

dt.Rows.Add(new object[]{fi.FileName});

:-)

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
SLODUG - Slovene Developer Users Group
www.rthand.com

W.G. Ryan eMVP said:
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("FileName", typeof(System.String));
dt.Columns.Add(dc);
foreach(FileInfo fi in Directory.GetDirectories(@"C:\")){
DataRow dro = dt.NewRow();
dro[0] = fi.FileName;
dt.Rows.Add(dro);

}

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
Sasha said:
Hi,

What is the easiest way to populate a DataTable with a list of files
from a folder?

Thank you,
Sasha
 
Amazing what laziness does for optimizations :-)

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
SLODUG - Slovene Developer Users Group
www.rthand.com

W.G. Ryan eMVP said:
Nice Catch Miha ;-)

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
Miha Markic said:
Even shorter one ;-)
Instead of
DataRow dro = dt.NewRow();
dro[0] = fi.FileName;
dt.Rows.Add(dro);

do

dt.Rows.Add(new object[]{fi.FileName});

:-)

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
SLODUG - Slovene Developer Users Group
www.rthand.com

W.G. Ryan eMVP said:
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("FileName", typeof(System.String));
dt.Columns.Add(dc);
foreach(FileInfo fi in Directory.GetDirectories(@"C:\")){
DataRow dro = dt.NewRow();
dro[0] = fi.FileName;
dt.Rows.Add(dro);

}

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
Hi,

What is the easiest way to populate a DataTable with a list of files
from a folder?

Thank you,
Sasha
 
Back
Top