File Directory ASP.Net Tutorial

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



    ViewFiles


    
    

        
            
                
                
            
        

                    
                    

                    
                    
                    

    
    

    


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