- 在线时间
- 8 小时
- 最后登录
- 2016-1-23
- 注册时间
- 2004-5-7
- 听众数
- 1
- 收听数
- 0
- 能力
- 0 分
- 体力
- 610 点
- 威望
- 0 点
- 阅读权限
- 30
- 积分
- 218
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 70
- 主题
- 26
- 精华
- 0
- 分享
- 0
- 好友
- 0
升级   59% TA的每日心情 | 怒 2014-2-22 20:49 |
|---|
签到天数: 13 天 [LV.3]偶尔看看II
群组: 2014美赛MCMA题备战群 群组: 2014美赛MCMB题备战群 |
查看子目录及文件(C#)
< >using System;) r' O. d" h, K5 ?/ k7 V. R! R- c
using System.IO;
w$ n' ^5 |1 D& e H: B! bnamespace ConsoleApplication1
, V4 Q( j: S6 O- e( a2 k' M) F{0 X8 P* I0 _8 Y. y9 m
/// <summary>
! h/ N4 D+ j; [2 o" o: O' z /// Class1 的摘要说明。2 R6 v: O4 q% V3 z X
/// </summary>: L1 r, q7 ~! `, h8 ~
class Class1
) J$ q8 V* M' f5 S6 m, c9 r: V/ ` { y8 r/ p' |4 [8 p. U3 X! r
/// <summary>* ^) A2 m+ r) E. y
/// 应用程序的主入口点。
+ Y- j$ c) [+ y7 w, }' b /// </summary>
# y- g& D- R1 \+ G [STAThread]
# \# [: C( \) V! P0 N' l. @ static void Main(string[] args)
9 H, {' y- J1 b+ R2 O5 t$ C3 H {3 G' N4 j. w" R6 q0 y: z
/// \' N4 ]# d; S8 A
// TOD 在此处添加代码以启动应用程序9 d) d% ~! b- v+ q
//</P>
5 r# r/ J4 @3 r< > Class1 t = new Class1();</P>
4 g" I2 ], P0 {< > string theDirectory = @"C:\WINDOWS";</P>( S. v* k* j( u9 H( `' _& v0 G
< > DirectoryInfo dir = new DirectoryInfo(theDirectory);</P>4 [' [# Q2 Z0 \3 {. I o
< > t.ExploreDirectory(dir);8 X5 n) X( r( u9 Y1 N) |
Console.WriteLine("\n\n{0} directories found.\n", dirCounter);
4 b8 P" g1 I L5 m" I E// Console.WriteLine("\n\n{0} files in {1} directories found.\n", fileCounter, dirCounter);1 i" m7 _, N9 v; b A
}</P>
1 P8 G) d. ~$ {. v1 C< > private void ExploreDirectory(DirectoryInfo dir)
% l0 U7 L, J0 P( D {8 T+ _# b4 L+ G% |
indentLevel++;</P>
! x$ ~+ m% \# V: W' N- r< > for (int i = 0; i < indentLevel; i++)8 M/ z% w2 j' G; c9 f% M/ t* V. C
Console.Write(" ");</P>
! O8 A& v& l/ x2 n4 i* q6 J< > Console.WriteLine("[{0}] {1} [{2}]\n", indentLevel, dir.Name, dir.LastWriteTime);</P>" J8 a, i0 u6 q' S1 c
< >// FileInfo[] fileInDir = dir.GetFiles();
% I7 m1 m, N, Z7 x1 Q. Y// foreach (FileInfo file in fileInDir)
& j- x$ L2 Q1 U. e f; t: }0 ?// {
' g! x& G" a( K" x6 Z8 S// for (int i = 0; i < indentLevel; i++)
/ m. n: y0 s/ @0 U! |// Console.Write(" ");
6 c" H# x4 f0 B3 x//
9 ~ ]; D X0 M0 ]0 ]. b// Console.WriteLine("{0} [{1}] Size: {2} bytes", file.Name, file.LastWriteTime, file.Length);
* o+ J% p# h5 x3 Z, v7 f. I8 O// fileCounter++;
- d3 d: j! s" d) P; n// }</P> G% d( R1 I5 X @
< > DirectoryInfo[] directories = dir.GetDirectories();
) _3 Z* ? l, t7 w' T# V ^& S foreach (DirectoryInfo newDir in directories)
7 a: _3 I. o! |# h+ Z' H {- V. S+ V5 Q* t/ |
dirCounter++;
. ~+ s, p/ q- d; o+ }. _8 Q; d ExploreDirectory(newDir);
" `* @" x/ o, U) P, E }</P>
3 C1 J! S" W4 m+ g8 u$ ^< > indentLevel--;
0 V* k4 E3 s8 x, s2 T4 |* M1 O }</P>; F% i8 i4 m0 {
< > private static int dirCounter = 1;//计数目录
8 l! ~( N2 b1 G1 S$ W! R private static int indentLevel = -1;//目录进缩
8 w" j/ `( O% r }// private static int fileCounter = 0;//计数文件( y# d+ i: w- H& {) y5 U4 {0 ~2 Y" k
}/ F8 |* d$ `3 C6 z# k' g
}</P> |
|