- 在线时间
- 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;7 _. k4 F4 {1 j) F0 R
using System.IO;
+ p1 C" e# }2 D2 E3 D! @namespace ConsoleApplication1
5 g& x( \" f& L6 t, l6 n% \{
! I0 ?. |0 n$ `% g /// <summary>
, O( ^) J' V: b' L) z /// Class1 的摘要说明。( K \* {0 x( w
/// </summary>: i7 e E: [! q K6 x8 h; |! E
class Class19 X; |! [- T2 H# L: Q; O7 n
{
# ^4 R& o3 B! u3 [6 d /// <summary>$ F3 l; n# O6 |5 ^( Z
/// 应用程序的主入口点。% z* @3 V" o+ Q- f X( B- \
/// </summary>3 R3 F5 B% f/ ~' c
[STAThread]$ A1 T3 }- _# s1 K% E3 s& l
static void Main(string[] args)7 H# W4 |* I1 E* s9 W' j$ q3 y
{7 @/ X7 Q" X* G; w# ~
//. K! z ]! f0 d
// TOD 在此处添加代码以启动应用程序: }$ o# B5 C6 F: U0 a! _
//</P>! \3 q5 w, {1 T" C* r4 u
< > Class1 t = new Class1();</P>& t! H" {, f* f: W6 G9 t
< > string theDirectory = @"C:\WINDOWS";</P>
- c; a3 T% p9 C& M2 o4 b- k# @8 K< > DirectoryInfo dir = new DirectoryInfo(theDirectory);</P>
. n. x/ h2 i2 v0 Y" E2 Z% r# d< > t.ExploreDirectory(dir);9 D% P2 K4 Z4 R$ w- y. A" y
Console.WriteLine("\n\n{0} directories found.\n", dirCounter);5 e. @7 N8 ~) E6 A( L
// Console.WriteLine("\n\n{0} files in {1} directories found.\n", fileCounter, dirCounter);+ a. t& r! J5 `& n4 b
}</P>
4 d# i/ U9 E+ ]< > private void ExploreDirectory(DirectoryInfo dir)0 T4 T ~4 O* N( i& Q
{0 B3 c8 B* k F3 q& i1 F
indentLevel++;</P>, U& B" n7 d3 L& y. I% ~
< > for (int i = 0; i < indentLevel; i++)
, J1 [1 p q6 b0 |7 { Console.Write(" ");</P>- H- l e* b4 [9 ^7 q
< > Console.WriteLine("[{0}] {1} [{2}]\n", indentLevel, dir.Name, dir.LastWriteTime);</P>) z$ e; U( W# L- {& p' T* e7 G% j& o
< >// FileInfo[] fileInDir = dir.GetFiles();7 ]0 b* ]7 F4 O6 m
// foreach (FileInfo file in fileInDir)4 H) Z$ P6 a+ V+ ~* r
// {
0 U* q3 a+ a0 T, k! f2 u3 L2 K// for (int i = 0; i < indentLevel; i++)) M) M) v; \ L9 ~% E
// Console.Write(" ");; t4 d. I+ g9 J d# A |
//
+ s! l2 `3 W# M$ p: m// Console.WriteLine("{0} [{1}] Size: {2} bytes", file.Name, file.LastWriteTime, file.Length);
. f- w9 H6 ]$ A+ x" x0 d, g6 j// fileCounter++;" w+ O8 J/ j" l; D& h6 B8 J" j5 n
// }</P>
5 I6 e) E, U. Q# z< > DirectoryInfo[] directories = dir.GetDirectories();
! E0 l4 c& J3 y" g5 P4 e& G foreach (DirectoryInfo newDir in directories)
" i8 P9 @1 J/ A0 G5 p {
2 Z* l' f" T3 Q i, Q dirCounter++;
! M1 o+ [* P1 T9 O1 q# @8 s" Z ExploreDirectory(newDir);
! \6 C, r" H* h! @" l! A }</P>+ V( |5 N3 [& d2 c: c) i9 [9 a
< > indentLevel--;. u) P! R: [$ W2 l7 p8 A) n ^4 C
}</P>8 M9 x* I, C; P% k; o' A
< > private static int dirCounter = 1;//计数目录2 X" p5 }/ U. U' N- j1 T/ n
private static int indentLevel = -1;//目录进缩% T t3 X! D5 n% G/ B/ y
// private static int fileCounter = 0;//计数文件" |/ Q, Q5 C. U/ X8 g! W5 O
}3 T6 {. _6 _( t1 g3 @' i: p# i: u4 h
}</P> |
|