Click here to Skip to main content
13,054,779 members (72,572 online)
Rate this:
Please Sign up or sign in to vote.
See more:
code for read all text file names in the folder and create excel file and file names send to excel in
Posted 11-May-13 0:52am
subbu a365
Raja Soosai 11-May-13 6:54am
What you have tried?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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#[^]
Maciej Los 11-May-13 8:01am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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[^]

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 11 May 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100