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);
// }
}
}
}