- 在线时间
- 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;
* l9 r+ y8 j! s( f, u# \- |6 Qusing System.IO;6 K- E8 \* E7 A8 p* ?* [
namespace ConsoleApplication1
% F! S+ i% j3 z' }{7 L( M& h$ O9 r1 D: s
/// <summary># q- n1 H. H V, D+ T) l
/// Class1 的摘要说明。
( `# D) I1 R! d' w+ o, G /// </summary> V5 O, S# C4 B) f) I
class Class1
8 t- x5 d9 c5 T {
" n$ c9 _$ S- c& q- a. `9 h /// <summary>0 Z1 T* D9 x7 q
/// 应用程序的主入口点。4 K- K7 f5 G6 c' s6 X" p
/// </summary>
5 F- N. H- X9 V$ _% {( o2 g3 q [STAThread]
2 q0 I0 H' v* [& o, x static void Main(string[] args)
! W- f, F. X& v7 Y$ H3 L$ |. Y {3 k, j2 ^; n5 A9 X" n
//
G3 e, y$ ^! ` S3 X0 j/ L1 |& q // TOD 在此处添加代码以启动应用程序
4 t9 V5 z$ Y+ ]! l. S7 _, Z //</P>
+ Q& a, E2 |! L< > Class1 t = new Class1();</P>, j$ }% v$ |4 G3 k! o
< > string theDirectory = @"C:\WINDOWS";</P>
" _! f) A) w7 Z: ^< > DirectoryInfo dir = new DirectoryInfo(theDirectory);</P>. R9 A0 ^. b+ n5 t
< > t.ExploreDirectory(dir);! k' x. }9 g1 m |% n
Console.WriteLine("\n\n{0} directories found.\n", dirCounter);
/ s2 }- k; d+ |5 B, j* K8 r// Console.WriteLine("\n\n{0} files in {1} directories found.\n", fileCounter, dirCounter);
2 S; p2 k, M" @+ f( Q5 y+ V }</P>, D6 \- N+ L2 V' h
< > private void ExploreDirectory(DirectoryInfo dir)
+ Z' |" G4 _' h1 ]3 }+ l& E {
& L; _5 z4 f2 y4 J indentLevel++;</P>
& v7 ~$ o6 E$ z- Z7 O< > for (int i = 0; i < indentLevel; i++)2 f @3 W# v9 w
Console.Write(" ");</P>3 h7 m: r" I" h3 {
< > Console.WriteLine("[{0}] {1} [{2}]\n", indentLevel, dir.Name, dir.LastWriteTime);</P>& p9 G% g2 n1 l# z
< >// FileInfo[] fileInDir = dir.GetFiles();1 N4 t e' V, L* X
// foreach (FileInfo file in fileInDir)
1 o3 _! f9 D% a0 R2 b// {7 G9 \( \4 }9 F) k2 @2 r
// for (int i = 0; i < indentLevel; i++)
i4 M6 ]: p2 u1 f8 F$ l& \// Console.Write(" ");+ b$ e/ c$ U- Y$ A' m2 I
//
" e) v, ^) `- q8 f$ t/ S. `// Console.WriteLine("{0} [{1}] Size: {2} bytes", file.Name, file.LastWriteTime, file.Length);
, p: R/ p; U' F! J// fileCounter++;4 y9 p3 O c; B- C7 y4 Z
// }</P>
+ B! O- u- [/ O8 X< > DirectoryInfo[] directories = dir.GetDirectories();9 S2 C! |& S- D1 ~$ _6 d
foreach (DirectoryInfo newDir in directories)
# _+ n# j3 f1 f! _0 }8 k { r A0 m; ?+ n2 S, d- p
dirCounter++;$ x* l/ \3 Q* j# L0 G) j1 y3 J% o
ExploreDirectory(newDir);
7 d B' k8 w' O$ C0 \. h9 V; g }</P>! {4 [3 }4 z4 v0 W2 j1 Q0 b
< > indentLevel--;! V6 Z8 u+ L( J. a. P5 f) ~4 x
}</P>
( K. E v5 |+ R! O5 o6 B" M5 V< > private static int dirCounter = 1;//计数目录) {4 s ~* M6 L u
private static int indentLevel = -1;//目录进缩
& u @9 Y3 x/ \$ q' C8 z- m// private static int fileCounter = 0;//计数文件/ l0 ^; [- x5 E4 o+ I3 ]
}
. y. X/ k) q k, z |8 D}</P> |
|