View file info (C#) : File Browser « Components « ASP.Net






View file info (C#)

<%@ Page language="c#" src="ViewFiles.aspx.cs" AutoEventWireup="false" Inherits="ViewFiles" %>
<HTML>
  <body>
    <form id="Form1" method="post" runat="server">
      <asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 24px" runat="server" Font-Names="Verdana" Font-Bold="True">Files in the FTP Directory:</asp:Label>
      <asp:ListBox id="lstFiles" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 56px" runat="server" Width="256px" Height="264px" AutoPostBack="True" Font-Names="Verdana"></asp:ListBox>
      <asp:Button id="cmdRefresh" style="Z-INDEX: 103; LEFT: 16px; POSITION: absolute; TOP: 328px" runat="server" Width="72px" Height="32px" Text="Refresh"></asp:Button>
      <asp:Label id="Label2" style="Z-INDEX: 104; LEFT: 296px; POSITION: absolute; TOP: 24px" runat="server" Width="248px" Height="24px" Font-Names="Verdana" Font-Bold="True">Selected File Information:</asp:Label>
      <asp:Label id="lblFileInfo" style="Z-INDEX: 105; LEFT: 296px; POSITION: absolute; TOP: 56px" runat="server" Width="312px" Height="264px" BorderStyle="Groove" BorderWidth="2px" Font-Names="Verdana" Font-Size="X-Small"></asp:Label>
      <asp:Button id="cmdDelete" style="Z-INDEX: 106; LEFT: 520px; POSITION: absolute; TOP: 328px" runat="server" Width="89px" Height="32px" Text="Delete File"></asp:Button>
    </form>
  </body>
</HTML>

<%--
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;

  public class ViewFiles : System.Web.UI.Page
  {
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.ListBox lstFiles;
    protected System.Web.UI.WebControls.Button cmdRefresh;
    protected System.Web.UI.WebControls.Label Label2;
    protected System.Web.UI.WebControls.Label lblFileInfo;
    protected System.Web.UI.WebControls.Button cmdDelete;

    string ftpDirectory = System.Web.HttpContext.Current.Server.MapPath("yourfolderHere");//@"c:\Temp";

    private void Page_Load(object sender, System.EventArgs e)
    {
      if (!this.IsPostBack)
      {
        CreateFileList();
      }
    }

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


    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
      InitializeComponent();
      base.OnInit(e);
    }
    
    private void InitializeComponent()
    {    
      this.lstFiles.SelectedIndexChanged += new System.EventHandler(this.lstFiles_SelectedIndexChanged);
      this.cmdRefresh.Click += new System.EventHandler(this.cmdRefresh_Click);
      this.cmdDelete.Click += new System.EventHandler(this.cmdDelete_Click);
      this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void cmdRefresh_Click(object sender, System.EventArgs e)
    {
          CreateFileList();
    }

    private void cmdDelete_Click(object sender, System.EventArgs e)
    {
    //  File.Delete(lstFiles.SelectedItem.Text);
      //CreateFileList();

    }

    private void lstFiles_SelectedIndexChanged(object sender, System.EventArgs e)
    {
      string fileName = lstFiles.SelectedItem.Text;
      lblFileInfo.Text = "<b>" + fileName + "</b><br>";
      lblFileInfo.Text += "Created : ";
      lblFileInfo.Text += File.GetCreationTime(fileName).ToString();
      lblFileInfo.Text += "<br>Last Accessed : ";
      lblFileInfo.Text += File.GetLastAccessTime(fileName).ToString();
      lblFileInfo.Text += "<br>";

      FileAttributes attributes = File.GetAttributes(fileName);
      if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
      {
        lblFileInfo.Text += "This is a hidden file." + "<br>";
      }
      if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
      {
        lblFileInfo.Text += "This is a read-only file." + "<br>";
      }

      if ((attributes & FileAttributes.ReadOnly) != FileAttributes.ReadOnly)
      {
        cmdDelete.Enabled = true;
      }
      else
      {
        cmdDelete.Enabled = false;
      }

    }
  }

--%>
           
       








Related examples in the same category

1.Get file list under control folder (C#)
2.Web based file browser (C#)