- 在线时间
- 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;* J6 E. B" i- x- S- P% A
using System.IO;
6 }2 @* S' W/ M, e3 D6 wnamespace ConsoleApplication1; w6 F3 ?7 o8 {( P
{. G6 M: {$ g0 k
/// <summary>6 V- V) |" W5 ?: @. C* K; {
/// Class1 的摘要说明。2 b* D6 m' h# C8 ]" y
/// </summary>' {" h4 t# T( v5 P; m2 F6 J
class Class1
0 A$ l8 ?* Q) W { d3 ^! ~+ ?: g! J; O& J5 [
/// <summary>* J5 G( A2 k( R, K4 J+ [
/// 应用程序的主入口点。
& @9 h+ X* [3 h- r3 A! b' @ /// </summary>
c. H4 V& I# t: p' A+ c0 ^9 p [STAThread]# k1 G d/ y6 L- {/ O
static void Main(string[] args)7 R- J( s+ I o8 d4 p$ |
{" @& s$ N6 A# \
//
% Y' V+ L, g3 @! r/ h6 B4 `% q; h+ c // TOD 在此处添加代码以启动应用程序
, F) b# z. L. c8 M5 H3 L //</P>) H" G) {5 n0 l( K( P
< > Class1 t = new Class1();</P>! [0 n' Q! I, I$ S- C
< > string theDirectory = @"C:\WINDOWS";</P>
. _0 ?9 B% q) n5 E5 z2 I< > DirectoryInfo dir = new DirectoryInfo(theDirectory);</P>
+ F) Q( f0 L3 Z7 J( h* k) I1 l< > t.ExploreDirectory(dir);
& ^1 {& o( _ R, W) q# F' C Console.WriteLine("\n\n{0} directories found.\n", dirCounter);
; Y; }0 q! D8 ~: u// Console.WriteLine("\n\n{0} files in {1} directories found.\n", fileCounter, dirCounter);
4 L$ D' |5 R$ i( q* s7 i% [ }</P> Z) K- ?- \' w
< > private void ExploreDirectory(DirectoryInfo dir)+ K2 Z! \" E x& y. j6 o5 ~
{, h" _ A- ]- J3 R; R
indentLevel++;</P>3 j0 s7 \! x4 N0 a& m+ B
< > for (int i = 0; i < indentLevel; i++)
/ J, \/ o5 o' O: a6 ?8 F Console.Write(" ");</P>
$ K$ n4 `: ~4 B< > Console.WriteLine("[{0}] {1} [{2}]\n", indentLevel, dir.Name, dir.LastWriteTime);</P>
s0 ] P7 k4 a4 [" ], M< >// FileInfo[] fileInDir = dir.GetFiles();( F. R% B9 x* X. [: D
// foreach (FileInfo file in fileInDir)# g: o4 K) }" F ^ t+ r
// {- P$ b! f/ T2 e! L r$ L% w1 W
// for (int i = 0; i < indentLevel; i++)
+ H# h5 G9 [2 q u9 y// Console.Write(" ");
: T: E0 n/ G/ I2 ]8 e9 X//
/ N) H4 L3 X' J// Console.WriteLine("{0} [{1}] Size: {2} bytes", file.Name, file.LastWriteTime, file.Length);
H) G! {7 Q0 r3 d- d* H/ {+ T// fileCounter++;5 H" g6 U4 b* Q8 h& V
// }</P>, S+ P- R0 r! X& E1 D" a( |
< > DirectoryInfo[] directories = dir.GetDirectories();; P1 B/ w. T- V+ S; u
foreach (DirectoryInfo newDir in directories)
7 A( ~ F$ N$ O6 \# V/ d% [ {1 }, S6 b8 ?) b4 h1 o
dirCounter++;
. E# E4 R& Z4 I1 T ExploreDirectory(newDir);1 i6 K: U* k( t+ a
}</P>
, E6 W3 T9 ] \0 n2 ?< > indentLevel--;
a/ X8 v, o2 H$ N }</P>
& F" D9 u x) U< > private static int dirCounter = 1;//计数目录4 R( @/ @ o7 Z7 B
private static int indentLevel = -1;//目录进缩
7 I! y7 j7 `4 B4 L" n' ?( a// private static int fileCounter = 0;//计数文件
( {# h4 ]+ d) c, V& q5 G }' K" b! _) k$ e7 w+ p
}</P> |
|