- 在线时间
- 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; d( o! f! |1 ]" X
using System.IO;+ C) N F0 o2 T9 w+ \
namespace ConsoleApplication14 X; B$ \3 `: q2 ~$ ^ e, T, K
{& k. j( Z: \4 j- v4 w
/// <summary>+ F) T% u. V" @" a* O6 q: g
/// Class1 的摘要说明。6 S n* d: F9 Z2 S0 h! y7 {
/// </summary>+ _! |/ ^. d* Q) q( s2 W9 a
class Class1
/ x3 i* W1 a( h3 g {
. H5 i' n. C/ n& Y }# ` /// <summary>
% t% m3 J' \: j$ n6 H /// 应用程序的主入口点。
3 D, z6 u% o" B3 b8 T2 t /// </summary>. ^5 K; h4 B, d# e; K
[STAThread]8 d8 v$ d, k, y/ q. w
static void Main(string[] args)" X+ J( _. \. c7 p+ Z7 w- R
{
/ g- x* n. R' Y# a' s/ \ //4 n) d- R) \3 S, h3 P8 m
// TOD 在此处添加代码以启动应用程序
* K$ O% u1 E) P) `" ] //</P>3 Q. q- r/ X- ]5 Q' \, U
< > Class1 t = new Class1();</P>% z; Y+ X$ v7 d; i* }
< > string theDirectory = @"C:\WINDOWS";</P>- l+ r* c# a3 A8 b) z6 m
< > DirectoryInfo dir = new DirectoryInfo(theDirectory);</P>) j; I% a( U- ?+ f) Z+ C( \
< > t.ExploreDirectory(dir);
; q% Q0 \( T1 n6 w5 N& x9 | Console.WriteLine("\n\n{0} directories found.\n", dirCounter);0 ~3 ~& U# J3 b- b
// Console.WriteLine("\n\n{0} files in {1} directories found.\n", fileCounter, dirCounter);
+ \# W) ^6 b) |& u9 d; X" l }</P>
& f5 W# i8 y4 C* |9 e/ }) z0 V. r< > private void ExploreDirectory(DirectoryInfo dir)
, S5 J7 [/ O+ K7 I {
9 T: D! O$ }6 o* |7 D indentLevel++;</P>
7 w$ }, X% Q# H! B: G* h6 [< > for (int i = 0; i < indentLevel; i++)
5 V) J: Q: Y( p Console.Write(" ");</P>
1 T) F% o- _/ |: `! J- w, [< > Console.WriteLine("[{0}] {1} [{2}]\n", indentLevel, dir.Name, dir.LastWriteTime);</P>
2 I- [+ g9 N& Q< >// FileInfo[] fileInDir = dir.GetFiles();' x7 A% Z h6 C) w$ V6 A2 F
// foreach (FileInfo file in fileInDir)
) y/ c+ [$ S; j! ]5 o' |" v// {& |2 \! U+ h+ @0 P; Z
// for (int i = 0; i < indentLevel; i++)7 @: r# K: d# ] D5 J: [, x$ m
// Console.Write(" ");
" u) u* F' R# \1 J) H//) Q8 E' C- ] y5 m' F0 Y) _
// Console.WriteLine("{0} [{1}] Size: {2} bytes", file.Name, file.LastWriteTime, file.Length);* _+ j0 Z+ g: i1 ?' w' B( t
// fileCounter++;5 q% Y3 k# A. _7 H* e% t5 I
// }</P>
, Q3 U7 t0 y2 @$ D< > DirectoryInfo[] directories = dir.GetDirectories();7 \: W0 _5 ~2 N6 B+ S: ]
foreach (DirectoryInfo newDir in directories)
4 d0 W# Z, `! w2 W" O! Q. V2 ? {2 I. B" Z: T8 M
dirCounter++;1 w1 C5 n7 H3 E5 i( a0 v' G4 A/ j
ExploreDirectory(newDir);* W3 I8 s- Y, I6 V% E9 @3 d J
}</P>) S, p# Y* }# |/ e
< > indentLevel--;
5 G7 _$ U i9 v }</P>
# ]" k/ b! u9 Y< > private static int dirCounter = 1;//计数目录 ~- h$ e/ L5 N" A
private static int indentLevel = -1;//目录进缩
/ i, ?1 A$ n4 b9 I% |7 _// private static int fileCounter = 0;//计数文件
5 l2 N$ l$ D+ w' H: p }
% G7 C+ B X! z8 u* g}</P> |
|