Web based file browser (C#) : File Browser « Components « ASP.Net






Web based file browser (C#)

<%@ Page language="c#" src="FileBrowser.aspx.cs" AutoEventWireup="false" Inherits="FileBrowser" %>
<HTML>
  <body>
    <form id="Form1" method="post" runat="server">
      <asp:ListBox id="lstDirs" style="Z-INDEX: 101; LEFT: 32px; POSITION: absolute; TOP: 72px" runat="server" Width="248px" Height="208px" Font-Names="Verdana"></asp:ListBox>
      <asp:Button id="cmdParent" style="Z-INDEX: 109; LEFT: 176px; POSITION: absolute; TOP: 280px" runat="server" Text="Up One Level" Width="100px" Height="24px"></asp:Button>
      <asp:Button id="cmdShowInfo" style="Z-INDEX: 107; LEFT: 312px; POSITION: absolute; TOP: 280px" runat="server" Text="Show Info"></asp:Button>
      <asp:Label id="Label2" style="Z-INDEX: 104; LEFT: 312px; POSITION: absolute; TOP: 56px" runat="server" Width="192px" Height="24px" Font-Names="Verdana" Font-Bold="True" Font-Size="X-Small">Contained Files:</asp:Label>
      <asp:ListBox id="lstFiles" style="Z-INDEX: 102; LEFT: 312px; POSITION: absolute; TOP: 72px" runat="server" Width="280px" Height="200px" Font-Names="Verdana"></asp:ListBox>
      <asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 32px; POSITION: absolute; TOP: 56px" runat="server" Width="216px" Height="38px" Font-Names="Verdana" Font-Bold="True" Font-Size="X-Small">Subdirectories:</asp:Label>
      <asp:Label id="Label3" style="Z-INDEX: 105; LEFT: 32px; POSITION: absolute; TOP: 16px" runat="server" Width="184px" Height="19px" Font-Names="Verdana" Font-Bold="True" Font-Size="Medium">Current Directory:</asp:Label>
      <asp:Button id="cmdBrowse" style="Z-INDEX: 106; LEFT: 32px; POSITION: absolute; TOP: 280px" runat="server" Text="Browse To Selected" Width="136px" Height="24px"></asp:Button>
      <asp:Label id="lblFileInfo" style="Z-INDEX: 108; LEFT: 312px; POSITION: absolute; TOP: 328px" runat="server" Width="280px" Height="80px" BorderStyle="Groove" BorderWidth="2px" Font-Names="Verdana" Font-Size="X-Small"></asp:Label>
      <asp:Label id="lblCurrentDir" style="Z-INDEX: 110; LEFT: 240px; POSITION: absolute; TOP: 16px" runat="server" Width="184px" Height="24px" Font-Names="Verdana" Font-Bold="True" Font-Size="Medium"></asp:Label>
    </form>
  </body>
</HTML>


<%-- FileBrowser.aspx.cs
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 FileBrowser : System.Web.UI.Page
  {
    protected System.Web.UI.WebControls.ListBox lstDirs;
    protected System.Web.UI.WebControls.Button cmdParent;
    protected System.Web.UI.WebControls.Button cmdShowInfo;
    protected System.Web.UI.WebControls.Label Label2;
    protected System.Web.UI.WebControls.ListBox lstFiles;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Label Label3;
    protected System.Web.UI.WebControls.Button cmdBrowse;
    protected System.Web.UI.WebControls.Label lblFileInfo;
    protected System.Web.UI.WebControls.Label lblCurrentDir;
  
    private void Page_Load(object sender, System.EventArgs e)
    {
      if (!this.IsPostBack)
      {
        string startingDir = System.Web.HttpContext.Current.Server.MapPath("yourfolderHere");//@"c:\Temp";
        lblCurrentDir.Text = startingDir;
        ShowFilesIn(startingDir);
        ShowDirectoriesIn(startingDir);
      }

    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
      //
      // CODEGEN: This call is required by the ASP.NET Web Form Designer.
      //
      InitializeComponent();
      base.OnInit(e);
    }
    
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
      this.cmdParent.Click += new System.EventHandler(this.cmdParent_Click);
      this.cmdShowInfo.Click += new System.EventHandler(this.cmdShowInfo_Click);
      this.cmdBrowse.Click += new System.EventHandler(this.cmdBrowse_Click);
      this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void ShowFilesIn(string dir)
    {
      DirectoryInfo dirInfo = new DirectoryInfo(dir);

      lstFiles.Items.Clear();
      foreach (FileInfo fileItem in dirInfo.GetFiles())
      {
        lstFiles.Items.Add(fileItem.Name);
      }
    }

    private void ShowDirectoriesIn(string dir)
    {
      DirectoryInfo dirInfo = new DirectoryInfo(dir);

      lstDirs.Items.Clear();
      foreach (DirectoryInfo dirItem in dirInfo.GetDirectories())
      {
        lstDirs.Items.Add(dirItem.Name);
      }
    }

    private void cmdShowInfo_Click(object sender, System.EventArgs e)
    {
      if (lstFiles.SelectedIndex != -1)
      {
        string fileName = Path.Combine(lblCurrentDir.Text, lstFiles.SelectedItem.Text);
        FileInfo selFile = new FileInfo(fileName);
        lblFileInfo.Text = "<b>" + selFile.Name + "</b><br>";
        lblFileInfo.Text += "Size: " + selFile.Length + "<br>";
        lblFileInfo.Text += "Created: ";
        lblFileInfo.Text += selFile.CreationTime.ToString();
        lblFileInfo.Text += "<br>Last Accessed: ";
        lblFileInfo.Text += selFile.LastAccessTime.ToString();
      }

    }

    private void cmdParent_Click(object sender, System.EventArgs e)
    {
      if (Directory.GetParent(lblCurrentDir.Text) != null){
        string newDir = Directory.GetParent(lblCurrentDir.Text).FullName;
        lblCurrentDir.Text = newDir;
        ShowFilesIn(newDir);
        ShowDirectoriesIn(newDir);
      }

    }

    private void cmdBrowse_Click(object sender, System.EventArgs e)
    {
      if (lstDirs.SelectedIndex != -1)
      {
        string newDir = Path.Combine(lblCurrentDir.Text, lstDirs.SelectedItem.Text);
        lblCurrentDir.Text = newDir;
        ShowFilesIn(newDir);
        ShowDirectoriesIn(newDir);
      }

    }

  }


--%>
           
       








Related examples in the same category

1.Get file list under control folder (C#)
2.View file info (C#)