CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

怎么得到指定磁盘下所有的文件夹

楼主gengxin_914(星星)2005-05-16 15:35:48 在 .NET技术 / C# 提问

我想得到一个磁盘下所有的文件夹该怎么写啊   ? 问题点数:30、回复次数:7Top

1 楼3tzjq(永不言弃)回复于 2005-05-16 15:53:25 得分 0

抄了一段来自MSDN的VB.Net代码:  
  Private   Sub   AddFiles(ByVal   nod   As   TreeNode)  
  Dim   strPath   As   String   =   nod.FullPath  
  Dim   strFile   As   String  
   
  With   nod  
  For   Each   strFile   In   Directory.GetFiles(strPath)  
  With   nod.Nodes.Add(Path.GetFileName(strFile))  
  .Tag   =   ItemType.File  
  End   With  
  Next  
  End   With  
  End   Sub  
   
  Private   Sub   AddFolders(ByVal   nod   As   TreeNode)  
  Dim   strPath   As   String   =   nod.FullPath  
  Dim   strDir   As   String  
   
  With   nod  
  For   Each   strDir   In   Directory.GetDirectories(strPath)  
  '   Path.GetFileName   returns   just   the   file   name   portion  
  '   of   the   full   path   returned   from   the   GetDirectories  
  '   method.  
  With   nod.Nodes.Add(Path.GetFileName(strDir))  
  '   Keep   track   that   this   item    
  '   is,   in   fact,   a   directory.   We'll   need  
  '   that   info   later.    
  .Tag   =   ItemType.Directory  
   
  '   Add   the   DUMMY   node,   so   the   +   sign   appears.  
  .Nodes.Add(DUMMY)  
  End   With  
  Next  
  End   With  
  End   Sub  
  Top

2 楼huangkc(软贱&henchman)回复于 2005-05-16 16:03:56 得分 10

string   strdir="路径";  
  DirectoryInfo   dircol;  
  DirectoryInfo   dirinfo   =   new   DirectoryInfo(strdir);  
  foreach( DirectoryInfo   dirinfo   in   dirinfo   .GetDirectories)  
  {  
  dircol=   (DirectoryInfo)dirinfo   ;    
  string   xxx=   dirdircol.Name   //目录名  
  ...  
   
   
  }Top

3 楼ljc_zy(彷徨)回复于 2005-05-16 16:14:02 得分 0

接分.Top

4 楼oyljerry(【勇敢的心】→ ㊣提拉米苏√㊣)回复于 2005-05-16 16:19:04 得分 0

CFileFind遍历Top

5 楼pingnt(淡蓝色的雪)回复于 2005-05-16 16:19:21 得分 0

Private   Sub   AddFiles(ByVal   nod   As   TreeNode)  
  Dim   strPath   As   String   =   nod.FullPath  
  Dim   strFile   As   String  
   
  With   nod  
  For   Each   strFile   In   Directory.GetFiles(strPath)  
  With   nod.Nodes.Add(Path.GetFileName(strFile))  
  .Tag   =   ItemType.File  
  End   With  
  Next  
  End   With  
  End   Sub  
   
  Private   Sub   AddFolders(ByVal   nod   As   TreeNode)  
  Dim   strPath   As   String   =   nod.FullPath  
  Dim   strDir   As   String  
   
  With   nod  
  For   Each   strDir   In   Directory.GetDirectories(strPath)  
  '   Path.GetFileName   returns   just   the   file   name   portion  
  '   of   the   full   path   returned   from   the   GetDirectories  
  '   method.  
  With   nod.Nodes.Add(Path.GetFileName(strDir))  
  '   Keep   track   that   this   item    
  '   is,   in   fact,   a   directory.   We'll   need  
  '   that   info   later.    
  .Tag   =   ItemType.Directory  
   
  '   Add   the   DUMMY   node,   so   the   +   sign   appears.  
  .Nodes.Add(DUMMY)  
  End   With  
  Next  
  End   With  
  End   Sub  
  Top

6 楼RockyZhang(Rocky)回复于 2005-05-16 16:43:06 得分 10

写一个递归,用System.IO.Directory.GetDirectory方法遍历.Top

7 楼wf5360308(冷月孤峰)回复于 2005-05-18 21:00:38 得分 10

用DirectoryInfo。  
  MSDN上面有列子的Top

相关问题

  • 怎样搜索指定文件夹下的所有一级子文件夹?
  • 如何遍历在指定文件夹及其子文件夹所有文件
  • 如何删除指定文件夹下所有内容?
  • 如何将一个文件夹下的所有文件剪切到另一个指定的文件夹?
  • 求C#复制指定文件夹下所有文件和文件夹的类写法
  • 如何删除指定目录下所有的文件,包括子文件夹????
  • 高手帮忙,怎样遍历指定文件夹中的所有文件?
  • 入门级问题:获取指定文件夹中所有文件 [100分]
  • 怎样删除指定目录下所有的文件和子文件夹
  • 如何最快遍历指定目录下的所有文件夹?

关键词

  • nod
  • strdir
  • strpath
  • directoryinfo
  • strfile
  • getfilename
  • nodes
  • directory
  • path
  • add

得分解答快速导航

  • 帖主:gengxin_914
  • huangkc
  • RockyZhang
  • wf5360308

相关链接

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

广告也精彩

反馈

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