- 在线时间
- 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;
# F) b7 k# {" Iusing System.IO;
7 G" o- {$ P' A7 Pnamespace ConsoleApplication1
4 N) }" p1 t ?. Y$ h' M+ A) j{1 z5 H9 ?/ h( r( J8 v9 q7 P' ]3 e' ?
/// <summary>
9 y( M! r4 g7 r$ O4 U /// Class1 的摘要说明。
1 J, ^8 R; @8 y. m% K& K7 @ /// </summary>3 L, i, m: Y5 A' l1 U; I: S! x8 \
class Class1
) p/ s" L+ h& c6 | {' f, X% d. E, U' i
/// <summary>
& b* d1 f- T0 ]1 N/ b /// 应用程序的主入口点。
9 G) N) Z3 Q4 }" {1 i) o$ y /// </summary>- B) `8 ~5 v A4 M- [
[STAThread]% Y2 O5 z3 s4 W# g) K: d
static void Main(string[] args)$ y& X% o- h. h0 O6 s( _
{0 H" t3 s; ]$ i F0 j1 q e' u5 W, a
/// h* b0 S# x1 e1 p9 c
// TOD 在此处添加代码以启动应用程序
. F* h# ?+ M6 f+ B; x. ` //</P>$ w' s3 c! i: ]1 @3 i1 f
< > Class1 t = new Class1();</P>
6 `" r$ v4 {7 t5 o2 Y) @) ^ S< > string theDirectory = @"C:\WINDOWS";</P> e9 S h# o- m( t. |' J
< > DirectoryInfo dir = new DirectoryInfo(theDirectory);</P>. r! ?6 t1 I3 d$ r* L: S
< > t.ExploreDirectory(dir);
- M' n* y6 ^5 J Console.WriteLine("\n\n{0} directories found.\n", dirCounter);5 H& O5 W" m& [
// Console.WriteLine("\n\n{0} files in {1} directories found.\n", fileCounter, dirCounter);
" t5 I+ x* a( Z4 } }</P>* w' p' @! J* ~. R9 ]3 }2 [
< > private void ExploreDirectory(DirectoryInfo dir)
8 V! z' g$ b! ?- N {6 h' I3 _8 w9 l- {* z, B+ c' q9 z
indentLevel++;</P>+ ? s. y8 m8 d; `" B# k
< > for (int i = 0; i < indentLevel; i++)
7 o9 Y1 K7 f/ t7 S( T Console.Write(" ");</P># @4 y& k) M# g; Z, T
< > Console.WriteLine("[{0}] {1} [{2}]\n", indentLevel, dir.Name, dir.LastWriteTime);</P>5 ?, Q7 w5 V: @' |: @# C
< >// FileInfo[] fileInDir = dir.GetFiles();
0 s: _+ Y9 h( O% i( v1 {' k// foreach (FileInfo file in fileInDir)* g& `$ @0 D6 J U6 I2 k: {5 U
// {/ ]& }5 N3 Y. g) P" p; |, f* a
// for (int i = 0; i < indentLevel; i++)4 R7 Q _2 {8 b) P: G
// Console.Write(" "); Y3 A; g$ q4 J$ f
//" M3 `; L% a$ ~( a& t; j8 |
// Console.WriteLine("{0} [{1}] Size: {2} bytes", file.Name, file.LastWriteTime, file.Length);4 C% Y% }" L a. o* S
// fileCounter++;" ]8 `4 F9 ?% t! A4 G3 O d9 y
// }</P>" K3 @& l; M0 _( I4 Z
< > DirectoryInfo[] directories = dir.GetDirectories();( D! B: z( V; {- o1 }& V( A
foreach (DirectoryInfo newDir in directories)# x `" |" o- S9 Z8 `
{, }. G; {& I: B
dirCounter++;
9 I- _: V! O) S ExploreDirectory(newDir);
4 G6 Q( G/ {" M; u5 Y, e. t }</P>
2 ?) T# O S! |! W5 J< > indentLevel--;! D' |2 u+ v v, P9 e1 O! S
}</P>
9 T0 L1 h3 Q% B& T- L* j< > private static int dirCounter = 1;//计数目录
/ \2 S8 Y P. \& O7 o private static int indentLevel = -1;//目录进缩 g; S i8 f" R S4 l
// private static int fileCounter = 0;//计数文件" P% g( ^$ \6 E8 K7 i" l6 [) _
}, r& G4 c! Y3 p3 D6 E
}</P> |
|