Click here to Skip to main content
15,791,055 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
code for read all text file names in the folder and create excel file and file names send to excel in
Raja Soosai 11-May-13 6:54am    
What you have tried?

Reading file names is pretty easy:
string[] files = Directory.GetFiles(@"D:\Temp");

If you want the filename without the path then add:
string[] justFiles = files.Select(f => Path.GetFileName(f)).ToArray();
string[] justFiles = files.Select(f => Path.GetFileNameWithoutExtension(f)).ToArray();

Saving to Excel is more complex, but this may help simplify it: Write Data to Excel using C#[^]
Share this answer
Maciej Los 11-May-13 8:01am    
Another way is to get all file names into DataTable[^] and than use CopyFromRecordset method[^] for MS Excel range.

//get files into datatable
string[] filePaths = Directory.GetFiles(@"F:\\Download\\", "*.txt",SearchOption.AllDirectories);
DataTable dt = new DataTable("Files");
DataColumn dc = new DataColumn("File", Type.GetType("String"));
for (int i = filePaths.GetLowerBound(0); i < filePaths.GetUpperBound(0); i++)
    DataRow dr = dt.NewRow();
    dr["File"] = filePaths[i];

//create new workbook and insert data using CopyFromRecordset method ;)
//follow the link:

Please, follow this link: How to transfer data to an Excel workbook by using Visual C# 2005 or Visual C# .NET[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900