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

Home
ASP.Net
1.ADO.net Database
2.Ajax
3.Asp Control
4.Collections
5.Components
6.Data Binding
7.Development
8.File Directory
9.HTML Control
10.Language Basics
11.Login Security
12.Mobile Control
13.Network
14.Page
15.Request
16.Response
17.Server
18.Session Cookie
19.Sitemap
20.Theme Style
21.User Control and Master Page
22.Validation by Control
23.Validation by Function
24.WebPart
25.WPF
26.XML
ASP.Net » Components » File Browser 
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#)
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.