SIGN UP MEMBER LOGIN:    
ARTICLE

Read All Text Files in a Folder and Merge Into an aspx Page

Posted by Prabhakar Parihar Articles | ASP.NET Programming April 04, 2011
In this article I am gathering all text files from a folder and merging them into a single aspx page. In this article I am using 45 text files; they are first read then combined into an aspx page; it's a fast way to assemble all files into a webpage.
Reader Level:




Prabhakar_9345_Image1.jpg

In this article I am gathering all text files from a folder and merging them into a single aspx page. In this article I am using 45 text files; they are first read then combined into an aspx page; it's a fast way to assemble all files into a webpage.

In the aspx Page

First add this namespace:

System.IO

On Page Load Event

Then write this code for the Page Load:

protected void Page_Load(object sender, EventArgs e)
    {
       
        DirectoryInfo dirinfo = new DirectoryInfo(Server.MapPath("TXTF/"));

        string validExtensions = "*.txt";
        string[] extFilter = validExtensions.Split(new char[] { ',' });
        ArrayList files = new ArrayList();

        foreach (string extension in extFilter)
        {           
            files.AddRange(dirinfo.GetFiles(extension));
            try
            {
                for (int i = 0; i < files.Count; i++)
                {
                    StreamReader fb;
                    string filename = files[i].ToString();
                    fb = File.OpenText(dirinfo +  filename);
                    string input = null;
                    while ((input = fb.ReadLine()) != null)
                    {
                     Response.Write(input + "</br>");                                             
                    }
                    fb.Close();
                    //return 0;
                }
                               
            }
            catch (Exception ex)
            {
                lblerror.Text = ex.ToString();

            }

        }

Whole Setup....

All the text files are in a TXTF folder. In the aspx page you can change the folder name, then if you want to fetch only one file you can comment out the loop ... But this code works really well....

Conclusion

In this article I am showing how to merge multiple text files into one webpage. I think it is helpfull for every one....
 

Login to add your contents and source code to this article
share this article :
post comment
 

Thank You . . .

Posted by Prabhakar Parihar Apr 08, 2011

m searching this type of code, Thanks Sir

Posted by surender bhyan Apr 04, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor