Server side File viewer : Utility « File Directory « ASP.NET Tutorial






<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ViewFiles" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ViewFiles</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    <asp:ListBox ID="lstFiles" runat="server" Height="272px" AutoPostBack="True" OnSelectedIndexChanged="lstFiles_SelectedIndexChanged"></asp:ListBox><br />
                    <asp:Button ID="cmdRefresh" runat="server" Text="Refresh" OnClick="cmdRefresh_Click" /></td>
                <td>
                    <asp:Label ID="lblFileInfo" runat="server"></asp:Label><br />
                    <br />
                    <asp:Button ID="cmdDelete" runat="server" Text="Delete" OnClick="cmdDelete_Click" /></td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>

File: Default.aspx.cs


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;

public partial class ViewFiles : System.Web.UI.Page
{
    private string ftpDirectory;

    protected void Page_Load(object sender, EventArgs e)
    {
        ftpDirectory = Path.Combine(Request.PhysicalApplicationPath,"YourFolder");

        if (!this.IsPostBack)
        {
            CreateFileList();
        }
    }

    private void CreateFileList()
    {
        try
        {
            string[] fileList = Directory.GetFiles(ftpDirectory);
            lstFiles.DataSource = fileList;
            lstFiles.DataBind();
            lblFileInfo.Text = "";            
        }
        catch (Exception err)
        {
            lblFileInfo.Text = err.Message;
        }
        cmdDelete.Enabled = false;
    }

    protected void cmdRefresh_Click(object sender, EventArgs e)
    {
        CreateFileList();
    }
    protected void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            StringBuilder displayText = new StringBuilder();
            string fileName = lstFiles.SelectedItem.Text;
            displayText.Append(fileName);
            displayText.Append(File.GetCreationTime(fileName).ToString());
            displayText.Append(File.GetLastAccessTime(fileName).ToString());

            FileAttributes attributes = File.GetAttributes(fileName);
            if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
            {
                displayText.Append("hidden");
            }
            if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
            {
                displayText.Append("read-only");
                cmdDelete.Enabled = false;
            }
            else
            {
                cmdDelete.Enabled = true;
            }

            lblFileInfo.Text = displayText.ToString();
        }
        catch (Exception err)
        {
            lblFileInfo.Text = err.Message;
            cmdDelete.Enabled = false;
        }
    }


    protected void cmdDelete_Click(object sender, EventArgs e)
    {
        try
        {
            File.Delete(lstFiles.SelectedItem.Text);
            CreateFileList();
        }
        catch (Exception err)
        {
            lblFileInfo.Text = err.Message;
        }
    }
}








10.19.Utility
10.19.1.Server side File viewer