C# TreeView指定文件夹下的图片浏览

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 Microsoft.Web.UI.WebControls;
using System.IO;

namespace treeviewtest
{
/// <summary>
/// treeviewtest 的摘要说明。
/// </summary>
public class treeviewtest : System.Web.UI.Page
{
   protected System.Web.UI.WebControls.Image Image1;
   protected Microsoft.Web.UI.WebControls.TreeView treeView1;
   private void Page_Load(object sender, System.EventArgs e)
   {
    // 在此处放置用户代码以初始化页面
    if(!this.IsPostBack)
    {
this.Image1.ImageUrl=@"D:\photos\火影\ecbbc2ded2bbbabd3e01.jpg";
     showtreeview();
    }
   }

   private void showtreeview()
   {
    try
    {
     string filepath=@"D:\photos";
     this.treeView1.Nodes.Clear();
     TreeNode td = new TreeNode();
     td.Text =filepath;
     treeView1.Nodes.Add(td);
     GetAllFiles(filepath,td);
     GetDir(td,filepath);
    }
    catch(Exception err)
    {
     string strErro=err.Message;
//     Response.Write(err.Message);
//     Response.Write(err.Source);
//     Response.Write(err.StackTrace);
    }
   }

   //获取文件名
   private void GetAllFiles(string path,TreeNode CurrentNode)
   {
    DirectoryInfo mainDir = new DirectoryInfo(path);
    try
    {
     foreach(FileInfo files in mainDir.GetFiles())
     {
      TreeNode td =new TreeNode();
      td.Text = files.Name;
      td.ID=files.FullName;
//      Response.Write(files.FullName);
      CurrentNode.Nodes.Add(td);
     }
    }
    catch(Exception err)
    {
     string strErro=err.Message;
//     Response.Write(err.Message);
//     Response.Write(err.Source);
//     Response.Write(err.StackTrace);
    }
   }

   //获取文件夹路径
   private void GetDir(TreeNode Currentnode,string path)
   {
    try
    {
     DirectoryInfo mainDir = new DirectoryInfo(path);
     foreach(DirectoryInfo d in mainDir.GetDirectories())
     {
      TreeNode td = new TreeNode();
      td.Text = d.Name;
//      td.ID=d.FullName;
//      Response.Write(d.FullName);
      Currentnode.Nodes.Add(td);
      GetAllFiles(path+@"\"+d.Name,td);
      GetDir(td,path+@"\"+d.Name);
     }
    }
    catch(Exception err)
    {
     string strErro=err.Message;
//     Response.Write(err.Message);
//     Response.Write(err.Source);
//     Response.Write(err.StackTrace);
    }
   }

你可能感兴趣的:(treeview)