- 在线时间
- 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;
$ ~; I; j% |# `* ~- |using System.IO;
" d: h/ H- y2 Dnamespace ConsoleApplication10 W# I+ D2 Z Q5 ~* l; K* j
{
/ X4 d( K+ ^9 R' \$ p /// <summary>6 W5 y% E8 ]# H6 i
/// Class1 的摘要说明。
( P2 g5 S& u+ _6 H: j1 N /// </summary>- M0 Q, o, L, g0 I5 y+ U9 o
class Class1
2 m+ s1 o; I9 U& h- |/ ]% P+ | {
5 Z3 U0 N7 B+ U! i /// <summary>8 u3 n& l0 u# _% v6 R2 R# Z
/// 应用程序的主入口点。
2 S: c" D$ K2 r, { /// </summary>
6 ]. }1 C! i$ ^; b+ z* }8 P( ` [STAThread]
' e3 G2 I+ Q1 ~" V/ K static void Main(string[] args)/ r6 P2 G& L2 A+ r. m
{( {) N! t) D7 ]5 a
//. V: `/ n& O) N; r9 k2 e; x
// TOD 在此处添加代码以启动应用程序
1 N2 Q5 b( M1 u; t- l! P0 i- y: U //</P>' W( K) O* N9 S+ q* L2 ]6 `) I% O
< > Class1 t = new Class1();</P>, x0 Q6 a% v' m
< > string theDirectory = @"C:\WINDOWS";</P>1 w/ Z* N a: Q0 q' ?# f2 i
< > DirectoryInfo dir = new DirectoryInfo(theDirectory);</P>- ]3 U- _% t* U# M/ r, |
< > t.ExploreDirectory(dir);7 O) d8 x0 c4 x9 _
Console.WriteLine("\n\n{0} directories found.\n", dirCounter);
{+ Y0 d% B) k. _6 F f0 }9 H// Console.WriteLine("\n\n{0} files in {1} directories found.\n", fileCounter, dirCounter);& B* _5 W" [; q' X2 Y M" X9 w0 U. M
}</P>0 v; s6 X; P8 Q, }
< > private void ExploreDirectory(DirectoryInfo dir)4 P( N8 `5 ]% c" D* }: L. {
{
' t2 k! \& j) S% r- S; p indentLevel++;</P>
o8 Z& t. C! V9 k4 }6 b! z< > for (int i = 0; i < indentLevel; i++)
3 @' ^& n5 c7 q6 @ Console.Write(" ");</P>
( ~* Q# F: B6 ?" w% c0 i< > Console.WriteLine("[{0}] {1} [{2}]\n", indentLevel, dir.Name, dir.LastWriteTime);</P>0 |, s9 P+ `1 ~4 u
< >// FileInfo[] fileInDir = dir.GetFiles();5 Y, F% I4 W ?9 E8 f
// foreach (FileInfo file in fileInDir); e5 y+ o/ D; f8 H' r4 j
// {
; H# E! t2 w/ P// for (int i = 0; i < indentLevel; i++)) U0 `, x' U+ f4 A9 \' A
// Console.Write(" ");
0 ` \3 ] [% C$ e; L8 X' g( {! f' q4 \//+ m8 {) J5 a T7 m" J0 U2 Z
// Console.WriteLine("{0} [{1}] Size: {2} bytes", file.Name, file.LastWriteTime, file.Length);6 T& Q& C/ R* a3 r
// fileCounter++;
( l0 ~2 f+ f9 H; e# z// }</P>5 U+ c4 \+ I" s' T6 d' x$ J x
< > DirectoryInfo[] directories = dir.GetDirectories();
& B% e; n/ R" a; h/ p9 k5 f foreach (DirectoryInfo newDir in directories)
# R6 q/ _0 L6 _* s& { {4 O4 H- c; I! o9 `- k: T" g
dirCounter++;
# W/ x# c% L5 C. U1 I0 x- f2 p ExploreDirectory(newDir);8 R; g7 T9 c& E% S0 b$ r& ~
}</P>1 g, _ y1 F [+ K% R
< > indentLevel--;; K# f9 K7 t" |' M3 N7 B
}</P>
. K# f" H0 }6 M8 S3 ]( [& Y, b< > private static int dirCounter = 1;//计数目录
R: _/ T1 L8 H8 _ private static int indentLevel = -1;//目录进缩8 V0 h7 b: E" U% I P8 M2 E
// private static int fileCounter = 0;//计数文件; ?# I& v! | B: K$ L/ Z& v, O
}
5 x' T5 O3 ~3 E( G& U}</P> |
|