Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Images on Liveview Control

0.00/5 (No votes)
6 Mar 2014 1  
Find Images from Sub Directories and Show in List View Control

Introduction

This is simple and very useful. There are few image files in my project directory and I need to find the files from a specific directory and sub directory, input is the directory name and the images to be shown in list view control.

Using the Code

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td>
                <asp:TextBox ID="txtpath" runat="server"></asp:TextBox>
            </td>
            <td>
                <asp:Button ID="btnpath" runat="server" 
                text="Path" OnClick="btnpath_Click"/>
                     </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:ListView ID="ListView1" runat="server">
                    <LayoutTemplate>
      <ol>
         <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
      </ol>
   </LayoutTemplate>
                    <ItemTemplate>
                         <asp:ImageButton ID="ImageButton1" 
                         runat="server" Width="50px" Height="50px"  
                         imageurl='<%#Bind("Name") %>'></asp:ImageButton>
                         
                    </ItemTemplate>
                </asp:ListView>
            </td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Collections;

public partial class FileManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
        protected void btnpath_Click(object sender, EventArgs e)
        {
            ArrayList list = new ArrayList();
    
            try
            {
                string searchpath = txtpath.Text;
                DirectoryInfo dir = new DirectoryInfo(Server.MapPath(@"~/"));
                DirectoryInfo[] di = dir.GetDirectories(searchpath,SearchOption.AllDirectories);
                FileInfo[] files = null;
                foreach (DirectoryInfo d in di)
                {
                     files= d.GetFiles("*.*", SearchOption.AllDirectories);
                }
                ArrayList list1 = new ArrayList();
                foreach (FileInfo file2 in files)
                {
                    string a = Server.MapPath(@"~/");
                    string b = file2.FullName.Replace(a, "~/");
                    string c = b.Replace("\\", "/");

                    list1.Add(c);                

                    list.Add(file2);
                }
            
                ListView1.DataSource = list;
                ListView1.DataBind();
                int _i = 0;
                foreach (ListViewItem li in ListView1.Items)
                {
                    Image im = (Image)li.FindControl("ImageButton1");
                    im.ImageUrl = list1[_i].ToString();
                    _i = _i + 1;
                }
            }
            catch(Exception ex)
            {

            }
        }
//

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here