数学建模社区-数学中国

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

作者: 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;
+ v$ _7 @/ k3 Q" o( H2 r( Tusing System.IO;
' {1 m- q6 V: n/ \6 znamespace ConsoleApplication1
1 j# S' y) }0 N, ^{3 c/ L+ {, a( ^6 r* M" d
/// &lt;summary&gt;
8 d, ~8 @+ b$ ~. E /// Class1 的摘要说明。/ H- O, u# m" p' c
/// &lt;/summary&gt;
9 k& H5 Y3 ^2 ? class Class1
! J4 g6 B( Q& M3 n1 N# B4 d {
9 M/ H8 N) }5 t7 Q- Z/ C& R  /// &lt;summary&gt;
6 R, V  D5 x6 u  /// 应用程序的主入口点。; J$ {' q( E; f# r# i2 ?
  /// &lt;/summary&gt;4 M) M& d! K9 `! {4 p
  [STAThread]! E9 P1 z9 {/ z; E$ v2 b+ \
  static void Main(string[] args)
; P8 R! d) c: ]' Z9 _: g6 ~/ Z  {
8 B2 X4 Y+ U+ |, k   //- C& ?% O$ U! s" n9 l
   // TOD 在此处添加代码以启动应用程序8 z: X8 Q7 G2 f  j1 e" r$ C
   //</P>
1 q& x6 v: T( _% k. T& Y$ _<>   Class1 t = new Class1();</P>
. _2 c, ]1 s3 Q" [( L+ \<>   string theDirectory = @"C:\WINDOWS";</P>
# A( R, h2 X" W7 g" C6 ]<>   DirectoryInfo dir = new DirectoryInfo(theDirectory);</P>
/ n9 v  z9 P8 n<>   t.ExploreDirectory(dir);# f! b: V7 |/ j8 J8 s8 U
   Console.WriteLine("\n\n{0} directories found.\n", dirCounter);7 H+ \# o+ n7 M1 d$ z5 L9 f. q
//   Console.WriteLine("\n\n{0} files in {1} directories found.\n", fileCounter, dirCounter);& A, D4 i. x6 Y
  }</P>3 D" ~' `6 i9 ?' h7 k  g
<>  private void ExploreDirectory(DirectoryInfo dir)8 T/ ^' Z* `% j- a3 A' r( e- `/ g
  {
" w# N! E% A# E1 f* k3 S) b   indentLevel++;</P>" d, [' s9 Z6 s* |8 a. K
<>   for (int i = 0; i &lt; indentLevel; i++)
- `* }  A6 ^+ C2 L: D: H    Console.Write("   ");</P>
) H; l, ?: l3 j; S' l3 V<>   Console.WriteLine("[{0}] {1} [{2}]\n", indentLevel, dir.Name, dir.LastWriteTime);</P>8 l. M6 \$ J6 _* n% G. s7 o: z& H4 j
<>//   FileInfo[] fileInDir = dir.GetFiles();
- b/ n. B& Q& s//   foreach (FileInfo file in fileInDir)$ L7 r: p; Y; W7 q  J
//   {7 Q6 M6 G0 {& U% I& z
//    for (int i = 0; i &lt; indentLevel; i++)
+ ]/ h' X! k: z5 H& A+ }6 H" ~5 B' i+ v//     Console.Write("   ");
8 }3 v2 \: X) E, U* H//
$ i: D7 ~9 P3 S, {: T0 C//    Console.WriteLine("{0} [{1}] Size: {2} bytes", file.Name, file.LastWriteTime, file.Length);
+ _7 ]4 e! z5 H! T//    fileCounter++;+ J2 o$ R  `9 H) i3 V
//   }</P>
9 R3 u) @* x' O& A, N5 Y% c<>   DirectoryInfo[] directories = dir.GetDirectories();
% G6 C: t$ ^& ~. f+ l# y) }4 V   foreach (DirectoryInfo newDir in directories), ]. y& P8 T; F, Z
   {. r' c8 E! \* H& d# d, F
    dirCounter++;1 o) z1 `$ x9 t! t/ R4 F, t
    ExploreDirectory(newDir);
9 _1 w8 b9 x$ x1 ~   }</P>/ ~+ ]; q3 i5 t* J: K% s
<>   indentLevel--;
" j' P& A  B5 a; U  }</P>
& Y) F% H! A% C9 Z& h<>  private static int dirCounter = 1;//计数目录
. `/ [2 }8 P( d9 p1 H; Z, Q  i( L  private static int indentLevel = -1;//目录进缩
6 M& q0 I/ V9 e//  private static int fileCounter = 0;//计数文件" E6 s, q& i6 E5 X! B" U' g
}: S' e+ Y4 @9 ?; P8 m. I  c! f" X
}</P>
作者: lckboy    时间: 2004-7-31 05:06
<>部分注释是查看文件夹中的文件,输出文件夹和文件的名字和创建时间,要查看文件,只要去掉注释就可以。</P>
( h6 Z( X, j( M$ v$ }<>另外,程序的部分代码是为输出到Console而写,你只要简单重构一下,即可得到适合你的代码。</P>[em01][em01][em01]
- \6 m8 D% y0 \+ @' T5 Z
[此贴子已经被作者于2004-7-31 5:15:13编辑过]

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




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