- 在线时间
- 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;
5 F! p1 \5 d( d8 O, x8 husing System.IO;: Y9 V4 X) N( u
namespace ConsoleApplication1
/ L; L# B. I2 H+ I2 v; [{6 ]& A/ ~7 D, @
/// <summary>7 O e) q6 Z8 h" P/ c& _
/// Class1 的摘要说明。
0 ^! g3 W; o4 \1 r) Z$ ^ /// </summary>/ W/ n' E1 O( v$ K1 V( j
class Class1
6 T) `& ^/ F! \! i {6 e0 f+ [% u2 F2 g0 f
/// <summary>/ `( a+ G* H7 E* _
/// 应用程序的主入口点。$ Q$ z5 f3 t* w0 s: g
/// </summary>9 @# a8 Y8 {( V4 k2 u x6 L
[STAThread]1 F+ U6 h+ E8 ?; m' h1 L
static void Main(string[] args)
, ^( ?1 o% g# ^ {
" f) M: @- A5 Q- ~8 v //
4 L! g/ Q; Q8 \& W. m- v1 r9 X // TOD 在此处添加代码以启动应用程序8 T* p* u+ f" T7 }/ D
//</P>
% s& M# }* D" i2 J2 _< > Class1 t = new Class1();</P>
* ^/ A& b( W7 `, [) S< > string theDirectory = @"C:\WINDOWS";</P>
0 z L5 H+ |$ o* _, o< > DirectoryInfo dir = new DirectoryInfo(theDirectory);</P>1 d3 F& @7 p- z' k+ a2 t/ J, n. L5 |
< > t.ExploreDirectory(dir);
' w% ^, j( K& u& p1 ]: y. q, ? Console.WriteLine("\n\n{0} directories found.\n", dirCounter);
v' G4 P7 p( I) d6 _/ Z// Console.WriteLine("\n\n{0} files in {1} directories found.\n", fileCounter, dirCounter);
: e& d, s1 x$ H }</P>
4 T% S! ]; v. H3 J' D< > private void ExploreDirectory(DirectoryInfo dir)5 Z3 ?% K5 N" L" a9 Q0 S
{( f g& V8 z' \6 Q- Z7 x: E( P
indentLevel++;</P>
% d+ H; e8 V, u) T< > for (int i = 0; i < indentLevel; i++)
# ^3 j. ~& K; D Q% Q Console.Write(" ");</P>
% u- E0 u- U/ ^0 D< > Console.WriteLine("[{0}] {1} [{2}]\n", indentLevel, dir.Name, dir.LastWriteTime);</P> i* j7 p5 Z1 E c
< >// FileInfo[] fileInDir = dir.GetFiles();8 e8 f# v9 n% \6 j& f; T' m8 ^
// foreach (FileInfo file in fileInDir)
6 Q! [0 A# T2 M// {5 E6 P) `2 r; Q- ?1 z( B3 Q
// for (int i = 0; i < indentLevel; i++)) K5 ~2 G% h% @! ~5 Y5 @
// Console.Write(" ");
5 X8 X5 C* ~( Y2 I; W2 f# @& j, `' a//+ t9 j: w# L; T" a
// Console.WriteLine("{0} [{1}] Size: {2} bytes", file.Name, file.LastWriteTime, file.Length);
- ?% d# q6 p$ r8 ~, q// fileCounter++;$ K. c1 {7 c$ G8 I% B- V, y b
// }</P>
^, D/ [8 @ ]; O5 Y< > DirectoryInfo[] directories = dir.GetDirectories();
1 u0 I4 l& a' L5 k5 \ foreach (DirectoryInfo newDir in directories) A1 b; Y, z L5 x9 K
{
# O+ z9 m6 E9 G: Q( V% S dirCounter++;+ p/ e# p5 P$ s; E6 r0 h. Y0 C0 J& V
ExploreDirectory(newDir);
4 h6 `1 E! f' ^" u }</P>
6 c% d7 s! o3 w) U/ i; ~7 Z2 j< > indentLevel--;! x% {- Y' E! x5 Q0 p7 J2 a
}</P># z. k* c! o0 O" S
< > private static int dirCounter = 1;//计数目录
( H- @1 l; l# [# E" Z private static int indentLevel = -1;//目录进缩# m3 W- @3 h' x1 q/ T0 I. g' N: j9 [
// private static int fileCounter = 0;//计数文件. @( l* ^3 q" o) b" C3 o2 l5 g" n
}
; k9 D& C) _0 Y$ p' {1 C( ]}</P> |
|