WebPad.cs

// Copyright � 2007 Jeffrey Bazinet, http://www.vwd-cms.com/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

namespace VwdCms.Admin
{
    public class WebPad : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Label lblInstructions;
        protected System.Web.UI.WebControls.TextBox txtFile;
        protected System.Web.UI.WebControls.TextBox txtText;
        protected System.Web.UI.WebControls.Label lblError;
        protected System.Web.UI.WebControls.Button btnOpen;
        protected System.Web.UI.WebControls.Button btnSave;

        protected void Page_Load(object sender, EventArgs e)
        {
            VwdCms.Configuration.Utilities.ProtectAdminPages();

            this.lblInstructions.Style.Add("display", "none");
            this.lblInstructions.Text = string.Empty;

            this.lblError.Visible = false;
            this.lblError.Text = string.Empty;

            if (!this.Page.IsPostBack)
            {
                string file = this.Page.Request.QueryString["file"];
                if (!string.IsNullOrEmpty(file))
                {
                    this.txtFile.Text = file;
                    string text = ReadFile(this.txtFile.Text);
                    this.txtText.Text = text;
                }
                else
                {
                    string lpApp = this.Server.MapPath("~/");
                    this.txtFile.Text = lpApp;
                }

                string msgid = this.Page.Request.QueryString["msgid"];
                if (!string.IsNullOrEmpty(msgid))
                {
                    string msg = string.Empty;

                    switch (msgid)
                    {
                        case "badxmlfile":
                            msg = "You have been redirected to WebPad to edit this XML file "
                                + "because it could not be parsed properly. Look for any obvious "
                                + "formatting errors and correct them and save your changes.";
                            break;
                    }

                    this.lblInstructions.Text = msg;
                    this.lblInstructions.Style.Add("display", "inline");
                }
            }

            this.btnOpen.Command += new CommandEventHandler(btnOpen_Command);
            this.btnSave.Command += new CommandEventHandler(btnSave_Command);
        }

        void btnSave_Command(object sender, CommandEventArgs e)
        {
            WriteFile(this.txtFile.Text, this.txtText.Text);
            System.Threading.Thread.Sleep(250);

            string text = ReadFile(this.txtFile.Text);
            this.txtText.Text = text;

        }

        void btnOpen_Command(object sender, CommandEventArgs e)
        {
            string text = ReadFile(this.txtFile.Text);
            this.txtText.Text = text;
        }

        private string ReadFile(string localpathfile)
        {
            string text = null;

            if (File.Exists(localpathfile))
            {
                StreamReader sr = null;

                try
                {
                    sr = new StreamReader(localpathfile);
                    text = sr.ReadToEnd();
                }
                catch (Exception ex)
                {
                    string msg = "Failed while trying to read the file '{0}'.\r\n\r\n";
                    msg = string.Format(msg, localpathfile) + ex.ToString();
                    this.lblError.Text += msg;
                    this.lblError.Visible = true;
                }
                finally
                {
                    if (sr != null)
                    {
                        sr.Close();
                        sr = null;
                    }
                }
            }
            else
            {
                string msg = "ERROR: The file that you entered does not exist.";
                this.lblError.Text += msg;
                this.lblError.Visible = true;
            }
            return text;
        }

        private void WriteFile(string localpathfile, string text)
        {
            try
            {
                VwdCms.Admin.IO.WriteFile(localpathfile, text);
            }
            catch (Exception ex)
            {
                string msg = "Failed while trying to write to the file '{0}'.\r\n\r\n";
                msg = string.Format(msg, localpathfile) + ex.ToString();
                this.lblError.Text += msg;
                this.lblError.Visible = true;
            }
        }
    }
}