ASP.NET custom control TextFileViewerControl

This control opens text file and shows it as plain text in ASP.NET page.
It optionaly can delete the file(if it was temporary) after the reading.

 The source code posted using CopySourceAsHtml

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.ComponentModel;

using System.IO;

using FSHelperLib;

namespace FSWeb.CustomControls

{

    /// <summary>

    /// Summary description for TextFileViewerControl.

    /// </summary>

    [DefaultProperty("Text"),

        ToolboxData("<{0}:TextFileViewerControl runat=server></{0}:TextFileViewerControl>")]

    public class TextFileViewerControl : System.Web.UI.WebControls.Panel

    {

    //    private string text;

 

        public TextFileViewerControl(): base()

        {m_deleteAfterReading=false;

        }

 

        [Bindable(true),

            Category("Data"),

            DefaultValue("")]

            // Properties

        public string FilePath

        {

            get

            {

                return this.m_sFilePath;

            }

            set

            {

                this.m_sFilePath = value;

            }

        }

 

 

        // Fields

        private string m_sFilePath;

 

        /// <summary>

        /// Property DeleteAfterReading (bool)

        /// </summary>

        [Category("Behaviour"),DefaultValue(false)]

        public bool DeleteAfterReading

        {

            get

            {

                return this.m_deleteAfterReading;

            }

            set

            {

                this.m_deleteAfterReading = value;

            }

        }

        private bool m_deleteAfterReading;

        /// <summary>

        /// Render this control to the output parameter specified.

        /// </summary>

        /// <param name="output"> The HTML writer to write out to </param>

        protected override void Render(HtmlTextWriter output)

        {

            output.Write(RenderTextFile(FilePath));

        }

        public string RenderTextFile(string sFilePath)

        {

            string sRet="";   

//            try

            {

                // Create an instance of StreamReader to read from a file.

                // The using statement also closes the StreamReader.

                using (StreamReader sr = new StreamReader(sFilePath) )

                {

                    String line;

                    // Read and display lines from the file until the end of

                    // the file is reached.

                    while ((line = sr.ReadLine()) != null)

                    {

                        sRet+=line+  HtmlHelper.NewLine;

                    }

                }

                if(DeleteAfterReading==true)

                    File.Delete(sFilePath);

                return sRet;

            }

//            catch (Exception ex)

//            {

//                throw new FileLoadException("The file could not be read:",sFilePath,ex);

//            }

        }

 

    }

}

posted @ Friday, May 16, 2008 10:43 AM

Print

Comments on this entry:

No comments posted yet.

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345