CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

treeView and Directory

楼主fancyhsq(hushiqi)2004-08-04 14:59:09 在 .NET技术 / C# 提问

怎么在treeView里完全显示一个Directory像openDirectory差不多的效果 问题点数:100、回复次数:4Top

1 楼newsea008(J-LOVE)回复于 2004-08-04 15:12:24 得分 70

using   System;  
  using   System.Collections;  
  using   System.Windows.Forms;  
   
  namespace   UpdateServer  
  {  
  ///   <summary>  
  ///   ClassDirectory    
  ///   </summary>  
  public   class   ClassDirectory  
  {  
  private ArrayList m_AFile   = new   ArrayList();  
  private ArrayList m_ADirectory   = new   ArrayList();  
  private DateTime m_DTModify   = new   DateTime();  
  private string m_strDirectory   = "";  
  private string m_strName = "";  
  public   ClassDirectory()  
  {  
  //  
  //   TODO:    
  //  
  }  
   
  public   void   SetDirectory(string   strDirectory)  
  {  
  m_strDirectory   =   strDirectory;  
  m_strName   =   GetDirectoryOrFileName(m_strDirectory);  
  m_AFile.Clear();  
  m_ADirectory.Clear();  
   
  System.IO.DirectoryInfo   dirInfo   =   new   System.IO.DirectoryInfo(m_strDirectory);  
  m_DTModify   =   dirInfo.LastWriteTime;  
  }  
   
  public   string   GetName()  
  {  
  return   m_strName;  
  }  
   
  private   string     GetDirectoryOrFileName(string   strPath)  
  {  
   
  //return   strPath.Substring(0,   strPath.IndexOf("\\"));  
  return   strPath.Substring(strPath.LastIndexOf("\\")   +   1,   strPath.Length   -   strPath.LastIndexOf("\\")   -   1);  
  }  
   
  public   string   GetDirectory()  
  {  
  return   m_strDirectory;  
  }  
   
  public   TreeNode   FillStuctTreeNode()  
  {  
  if(m_strDirectory.Length   <=   0)  
  return   null;  
   
  TreeNode   newNode   =   new   TreeNode(GetDirectoryOrFileName(m_strDirectory));  
  newNode.Tag   =   m_strDirectory;  
   
  string[]   dirs   =   System.IO.Directory.GetDirectories(m_strDirectory);  
  foreach(string   dir   in   dirs)  
  {  
  ClassDirectory   subDir   =   new   ClassDirectory();  
  subDir.SetDirectory(dir);  
  newNode.Nodes.Add(subDir.FillStuctTreeNode());  
  m_ADirectory.Add(subDir);  
  }  
   
  string[]   files   =   System.IO.Directory.GetFiles(m_strDirectory);  
  foreach(string   file   in   files)  
  {  
  ClassFile   subFile   =   new   ClassFile();  
  subFile.SetPath(file);  
   
  newNode.Nodes.Add(subFile.FillStructTreeNode());  
  m_AFile.Add(subFile);  
  }  
   
  return   newNode;  
  }  
   
  // private   string     GetDirectoryOrFileName(string   strPath)  
  // {  
  // return   strPath.Substring(strPath.LastIndexOf("\\")   +   1,   strPath.Length   -   strPath.LastIndexOf("\\")   -   1);  
  // }  
   
  public   string   GetNext(string   strPrevious)  
  {  
  string   strNext   =   null;  
  if(strPrevious   ==   null)  
  {  
  for(int   i   =   0;   i   <   m_ADirectory.Count;   i++)  
  {  
  strNext   =   ((ClassDirectory)(m_ADirectory[i])).GetNext(strPrevious);  
  if(strNext   !=   null)  
  break;  
  }  
  if(strNext   ==   null)  
  {  
  if(m_AFile.Count   >   0)  
  strNext   =   ((ClassFile)(m_AFile[0])).GetTransString();  
  }  
  }  
  else  
  {  
   
  strPrevious   =   strPrevious.Substring(GetName().Length   +   1,   strPrevious.Length   -   GetName().Length   -   1);  
   
   
  if(strPrevious.IndexOf("\\")   >=   0)  
  {  
  int   i   =   0;    
  ClassDirectory   CD   =   null;  
  for(i   =   0;   i   <   m_ADirectory.Count;   i++)  
  {  
  CD   =   (ClassDirectory)(m_ADirectory[i]);  
  if(strPrevious.Substring(0,   strPrevious.IndexOf("\\")).CompareTo(CD.GetName())   ==   0)  
  {  
  break;  
  }  
  }  
  if(i   >=   m_ADirectory.Count)  
  strNext   =   null;  
  else  
  {  
  strNext   =   CD.GetNext(strPrevious);  
  if(strNext   ==   null)  
  {  
  ClassFile   CF   =   null;  
  for(int   j   =   0;   j   <   m_AFile.Count;   j++)  
  {  
  CF   =   (ClassFile)(m_AFile[j]);  
  strNext   =   CF.GetTransString();  
  if(strNext   !=   null)  
  break;  
  }  
  }  
  }  
  }  
  else&#63730;  
  {  
  int   i   =   0;  
  ClassFile   CF   =   null;  
  for(i   =   0;   i   <   m_AFile.Count;   i++)  
  {  
  CF   =   (ClassFile)(m_AFile[i]);  
  if(strPrevious.CompareTo(CF.GetTransString())   ==   0)  
  {  
  break;  
  }  
  }  
   
  if(i   <   m_AFile.Count)  
  {  
  for(i++;   i   <   m_AFile.Count;   i++)  
  {  
  CF   =   (ClassFile)(m_AFile[i]);  
  strNext   =   CF.GetTransString();  
  if(strNext   !=   null)  
  {  
  strNext   =   CF.GetTransString();  
  break;  
  }  
  }  
  }  
  }  
  }  
   
  if(strNext   !=   null)  
  strNext   =   GetName()   +   "\\"   +   strNext;  
   
  return   strNext;  
  }  
  }  
  }  
  作了一个   类,你可以看看。呵呵,对你也许有用。Top

2 楼daou101(海天一鸥)回复于 2004-08-04 15:42:59 得分 20

这里有个完整的TreeView实现的文件查看器的例子:  
   
  http://www.codeproject.com/csharp/filesentry.asp  
  Top

3 楼csxtu(就为了混口饭吃还不行嘛(做人要后到))回复于 2004-08-04 15:52:34 得分 10

我就知道会有现成的例子咯。Top

4 楼fancyhsq(hushiqi)回复于 2004-08-04 16:40:21 得分 0

好长,,,头晕Top

相关问题

  • TreeView?
  • Treeview??
  • treeview
  • treeview
  • TREEVIEW
  • treeview
  • treeview
  • TreeView
  • TreeView
  • about TreeView

关键词

  • null
  • strdirectory
  • strprevious
  • classdirectory
  • strnext
  • strpath
  • adirectory
  • afile
  • strname
  • getname

得分解答快速导航

  • 帖主:fancyhsq
  • newsea008
  • daou101
  • csxtu

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo