数学建模社区-数学中国

标题: 请问如何得到一个目录下的所有子目录的名字???? [打印本页]

作者: lixin    时间: 2004-7-26 08:41
标题: 请问如何得到一个目录下的所有子目录的名字????
请问如何得到一个目录下的所有子目录的名字????
作者: ilikenba    时间: 2004-7-26 10:43
<>你可以参考这个</P>http://www.matrixstudio.org/bbs/dispbbs.asp?boardID=10&amp;ID=34&amp;page=4
作者: lckboy    时间: 2004-7-31 05:05
标题: 查看子目录及文件(C#)
<>using System;( a; L: H2 @- _2 R  a, M  A4 h  g
using System.IO;# n, ~0 g0 d& D. m5 R
namespace ConsoleApplication18 G% k/ Y3 o  h+ M
{
+ I# _6 U$ x' r! x /// &lt;summary&gt;! h6 F2 w& Y; }" u, J
/// Class1 的摘要说明。
: I& C! i5 }9 h. [( W1 a /// &lt;/summary&gt;$ R3 p# R3 L0 u5 W' D7 f% d
class Class1. b& k3 ^, F4 j2 b
{9 Z' s4 s4 @' X6 K/ t& G
  /// &lt;summary&gt;# |8 i+ l& u& W  i  H3 }: C
  /// 应用程序的主入口点。- Q  |  g) n, ^& x: y
  /// &lt;/summary&gt;
, C+ g1 M; R* y9 ?  [STAThread]& m* r9 \  p# ]* A7 I" h8 G+ r
  static void Main(string[] args)" G# e: T% Y. v- o' s/ z; c
  {/ W* @# a6 I. g6 m
   //
6 {: e4 K8 T: F. n   // TOD 在此处添加代码以启动应用程序$ H. B4 h, q6 u
   //</P>6 n0 B$ S8 o* K4 ?# f7 W+ l
<>   Class1 t = new Class1();</P>1 y: I. k& o" x' f" D
<>   string theDirectory = @"C:\WINDOWS";</P>* Q) o. ~, g4 n; U: T) G. ?6 Q
<>   DirectoryInfo dir = new DirectoryInfo(theDirectory);</P>0 {1 {/ ^& }( M4 F1 P$ R3 U
<>   t.ExploreDirectory(dir);9 B  K5 [$ g7 B: w
   Console.WriteLine("\n\n{0} directories found.\n", dirCounter);
  X' D/ K& `2 T* Q% G! s//   Console.WriteLine("\n\n{0} files in {1} directories found.\n", fileCounter, dirCounter);; I3 R8 f& }# y" G0 a/ E8 Y1 z1 K
  }</P>
- f& `8 |5 B5 d( F! u1 d7 d; G<>  private void ExploreDirectory(DirectoryInfo dir), W* |& t( |3 |& ^5 Y  y0 @
  {8 k* y* O8 X& _4 y
   indentLevel++;</P>
* f8 @0 g7 D# d6 y/ h! {<>   for (int i = 0; i &lt; indentLevel; i++)6 D- F  p+ [1 d' Q8 @4 [# h/ E
    Console.Write("   ");</P>; G4 q4 H7 K) K9 d, b* q
<>   Console.WriteLine("[{0}] {1} [{2}]\n", indentLevel, dir.Name, dir.LastWriteTime);</P>+ K" E6 r& u0 F2 R
<>//   FileInfo[] fileInDir = dir.GetFiles();3 A$ _4 D$ H- p7 X, l7 X
//   foreach (FileInfo file in fileInDir)
# X/ _0 B  T- e: w/ J3 ^//   {
. L/ e  u  V# w0 D7 W# \4 L0 N//    for (int i = 0; i &lt; indentLevel; i++)
  O/ L# q7 Q+ p2 E. V//     Console.Write("   ");
+ M4 y" ^/ X+ V6 O# q) ?. d9 Y//1 |2 C) }, y5 B1 _5 U8 }
//    Console.WriteLine("{0} [{1}] Size: {2} bytes", file.Name, file.LastWriteTime, file.Length);- [& f2 L: Q  l. Z
//    fileCounter++;! k# P; K1 j( l4 I
//   }</P>3 s/ A" w( U6 z
<>   DirectoryInfo[] directories = dir.GetDirectories();
3 c: b6 l- }# D   foreach (DirectoryInfo newDir in directories)
( \0 N9 Y$ o' U! L" k   {
3 e! b  b4 S, z3 F    dirCounter++;
0 Q7 {( q* M* Q8 D( r    ExploreDirectory(newDir);* D& e3 S; O) _3 Q' s
   }</P># _0 g5 |+ G% T; r9 ^, p
<>   indentLevel--;# b- B7 \4 M, A+ |; _6 E
  }</P>
8 s+ `) o8 o: m9 H1 m" q<>  private static int dirCounter = 1;//计数目录
7 v* F$ H& I1 _7 E% I  private static int indentLevel = -1;//目录进缩
2 P* s1 ?( n' h//  private static int fileCounter = 0;//计数文件
9 ^6 I  p( N. J0 r, _ }! o: [0 c8 V1 D. V
}</P>
作者: lckboy    时间: 2004-7-31 05:06
<>部分注释是查看文件夹中的文件,输出文件夹和文件的名字和创建时间,要查看文件,只要去掉注释就可以。</P>
  G0 s" u, ^; I<>另外,程序的部分代码是为输出到Console而写,你只要简单重构一下,即可得到适合你的代码。</P>[em01][em01][em01]9 t3 }7 W4 q, u; \6 ?& L# B
[此贴子已经被作者于2004-7-31 5:15:13编辑过]

作者: xShandow    时间: 2004-9-9 10:58
有C#高手?




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5