0

I am looking for a way, if possible, to loop through a sub folder populated with multiple HTML files (snippets), then outputting the contents of these files within the one ASP.NET application page.

So if a new snippet was added to the sub folder, the application would automatically update, outputting its contents.

1
  • 1
    are you trying add html parts in the aspx page with the contents in sub folder? do you have a sample scenario? what have you tried till now? Commented Mar 11, 2015 at 11:29

1 Answer 1

1

I think you need this>

ASPX:

<asp:Literal runat="server" ID="lthtml"></asp:Literal>

CS:

 protected void Page_Load(object sender, EventArgs e)
    {
        string[] files = Directory.GetFiles("directory", "*.html");
        StringBuilder htmlContent = new StringBuilder();
        foreach (string f in files)
        {

            htmlContent.Append(ReadHtmlFile(f));
        }

        lthtml.Text = htmlContent.ToString();
    }


   public static StringBuilder ReadHtmlFile(string htmlFileNameWithPath)
   {
            System.Text.StringBuilder htmlContent = new    System.Text.StringBuilder();
            string line;
            try
            {
                using (System.IO.StreamReader htmlReader = new System.IO.StreamReader(htmlFileNameWithPath))
                {

                    while ((line = htmlReader.ReadLine()) != null)
                    {
                        htmlContent.Append(line);
                    }
                }
            }
            catch (Exception objError)
            {
                throw objError;
            }

            return htmlContent;
        }
Sign up to request clarification or add additional context in comments.

1 Comment

This worked a treat, thank you. I only had to amend the folder path to be relative to the application. Server.MapPath("~/directory")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.