数学建模社区-数学中国

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

作者: 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;
/ B8 d/ h( ^2 p! Z4 P6 U% m5 U  Husing System.IO;, j- ^8 K, v$ g4 I, C
namespace ConsoleApplication1
5 m9 L/ [1 S2 L, R( ~1 y9 V; @{3 H# ^1 r. M3 ^1 ?+ a- B1 i' l# t% F
/// &lt;summary&gt;
) M* r. V6 |5 {0 M& O! I6 E /// Class1 的摘要说明。
( p! {6 B* t2 q /// &lt;/summary&gt;
9 W' Y" ]; L* w% v" p8 T2 b# ~" @ class Class1
& ]# Y6 [+ b- \3 |( q {
) }& P6 L; w$ V% b" S  /// &lt;summary&gt;
/ K! j4 [, O' Z  /// 应用程序的主入口点。
- T$ t) {/ w8 O  /// &lt;/summary&gt;
2 j+ e) m! A7 o  [STAThread]+ u. c: y, f  |! P
  static void Main(string[] args)
. b# B9 Q+ V( p. m( R+ H5 T4 A  {- Y( H8 i2 \# Q) v4 b. z
   //& N6 ^3 V; E3 R/ ^) Q
   // TOD 在此处添加代码以启动应用程序
5 c$ ?6 C1 U1 A, G4 `; v   //</P>
5 D& ~. k- a4 Z$ Y4 X9 [<>   Class1 t = new Class1();</P>
  B4 W* P& W( v<>   string theDirectory = @"C:\WINDOWS";</P>
  T/ U8 g  \2 c% ~/ W<>   DirectoryInfo dir = new DirectoryInfo(theDirectory);</P>
+ f/ ^' ~; o' e8 x) c<>   t.ExploreDirectory(dir);* m% X- [3 w+ {1 }2 v
   Console.WriteLine("\n\n{0} directories found.\n", dirCounter);
6 s  O3 g, L4 O6 V5 c; n//   Console.WriteLine("\n\n{0} files in {1} directories found.\n", fileCounter, dirCounter);
( t; n. P; D( U7 c- ~  }</P>
! @6 ]! M5 E' d- j<>  private void ExploreDirectory(DirectoryInfo dir)
9 M% ^3 [: g! U: F  {
0 F  O) p: g( ^   indentLevel++;</P>
& c  V/ G; h4 h, m1 T4 @# V<>   for (int i = 0; i &lt; indentLevel; i++)' _5 G% l8 z" ?$ w9 K' f) D
    Console.Write("   ");</P>
0 Y" Z# h7 K/ b3 G1 z$ T<>   Console.WriteLine("[{0}] {1} [{2}]\n", indentLevel, dir.Name, dir.LastWriteTime);</P>
( |; N, X, {6 ^5 L8 y<>//   FileInfo[] fileInDir = dir.GetFiles();
5 t( G! v9 A$ ]//   foreach (FileInfo file in fileInDir)
3 z$ Y' t1 c: v  a6 z5 T//   {
. |$ u5 [/ S. N& Q; p' D% O% W6 u//    for (int i = 0; i &lt; indentLevel; i++)# P3 m: E" w. Q, ~. a4 O
//     Console.Write("   ");
& r2 c" u* H( m* e! W//
. S; h* A& y  u* d8 s, Y" \* J//    Console.WriteLine("{0} [{1}] Size: {2} bytes", file.Name, file.LastWriteTime, file.Length);
5 u" X; a& W5 ]$ N1 z//    fileCounter++;; a9 E7 ?7 w1 V' f
//   }</P>
: {4 I% I6 x* G8 D9 a; s0 f<>   DirectoryInfo[] directories = dir.GetDirectories();4 y  H+ F; j. g4 U
   foreach (DirectoryInfo newDir in directories)
" O2 t# T9 b; ]- T# Y- T   {1 m- _, O$ ]. \6 Y( V" g
    dirCounter++;- c# N* u/ F/ v
    ExploreDirectory(newDir);. w; e7 f3 c! x
   }</P>
& h  q% z4 B; f' a<>   indentLevel--;: k7 J2 ]' i& e
  }</P>" d6 Y3 a! [& A. v& z; m
<>  private static int dirCounter = 1;//计数目录3 B  b4 C  E6 p: u" W# n9 i
  private static int indentLevel = -1;//目录进缩! O; I# C9 U% E- Z: l% ]) O1 P* T( ]6 o
//  private static int fileCounter = 0;//计数文件% o/ b3 ^! J! c/ O2 L$ L, `7 Q
}" |/ u. d3 U& S) j# G
}</P>
作者: lckboy    时间: 2004-7-31 05:06
<>部分注释是查看文件夹中的文件,输出文件夹和文件的名字和创建时间,要查看文件,只要去掉注释就可以。</P>
8 k  l& F2 [' @# v. N1 k  n<>另外,程序的部分代码是为输出到Console而写,你只要简单重构一下,即可得到适合你的代码。</P>[em01][em01][em01]
+ ]2 S3 y( Z; i
[此贴子已经被作者于2004-7-31 5:15:13编辑过]

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




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