- 在线时间
- 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;
- ?9 P; j7 }2 @' s _using System.IO;
, O+ q. |4 h+ [, Q) c/ fnamespace ConsoleApplication11 l- v" v/ ]* n+ l; j
{5 d: X2 X/ s% M8 j; D: `# z
/// <summary># c5 _( h3 O; O" W
/// Class1 的摘要说明。
' [0 ]4 E+ P6 @4 a8 B' s: w /// </summary>" O5 e/ z2 b( @) o: D6 o# r
class Class1: O, I& }- t6 g( w
{
% Z+ U% @; Z4 b% j: l /// <summary>3 L7 X# T$ N# M8 `. L3 B8 A9 p
/// 应用程序的主入口点。
X/ |8 Y$ C: ~* S } /// </summary>
8 r9 q0 M1 w" r4 g. p9 s" M- l3 K [STAThread]9 q5 A# e& v" _7 ], ^
static void Main(string[] args)
% r0 u. `/ w3 b1 B" F6 e {3 q" D9 Q/ y9 v5 D4 f
//
6 c7 D+ K i# E$ G, D* ` // TOD 在此处添加代码以启动应用程序
3 n! x. K% P7 p4 l' x. t //</P>
; |- Y$ Q2 I/ y* t! b/ c& e< > Class1 t = new Class1();</P>4 }# d/ V4 {$ M' i" E: ^2 _. c
< > string theDirectory = @"C:\WINDOWS";</P>
& D# E {% p4 W; \5 Z" k% ]* |: u! @< > DirectoryInfo dir = new DirectoryInfo(theDirectory);</P>
9 @" K1 m" I$ z: i# \0 f8 x< > t.ExploreDirectory(dir);6 U7 o4 z& t% V5 l
Console.WriteLine("\n\n{0} directories found.\n", dirCounter);
@( a, o! A$ H4 T, Y// Console.WriteLine("\n\n{0} files in {1} directories found.\n", fileCounter, dirCounter);
- @5 [! B, h9 ~ }</P>
# R) f+ S# L. E4 Z# K< > private void ExploreDirectory(DirectoryInfo dir)
* e! \0 F* T b4 @& J6 ? {4 ?* a* }+ n2 d0 A8 g V' g
indentLevel++;</P>0 H: Y" S- ^) N" U7 C+ G/ `
< > for (int i = 0; i < indentLevel; i++)
0 y2 W3 U/ f# c. e7 L, |( L Console.Write(" ");</P>
2 f# d; S+ f) U7 J3 o" J< > Console.WriteLine("[{0}] {1} [{2}]\n", indentLevel, dir.Name, dir.LastWriteTime);</P>
$ b+ `' V: `' A; f/ R< >// FileInfo[] fileInDir = dir.GetFiles();
: E# r" H5 N' R* v. U& ?1 k$ Y# d/ v// foreach (FileInfo file in fileInDir)% `3 T. ], F& m5 E
// {
. P. ^( ?9 G3 w! f- G// for (int i = 0; i < indentLevel; i++)
4 m, v2 j2 B5 R5 U/ U// Console.Write(" ");+ ~- s7 l5 n6 r' ?. y
//
6 [5 g3 C" A6 g# M% s: d// Console.WriteLine("{0} [{1}] Size: {2} bytes", file.Name, file.LastWriteTime, file.Length);/ A3 v6 b: ~$ d( f$ x9 h- ?/ b6 v
// fileCounter++;
7 e" _8 Q% \5 F9 S& [9 L/ y E// }</P>
; V! c# N) N `* L/ H* a< > DirectoryInfo[] directories = dir.GetDirectories();9 O; x# M/ Q, k5 z' U7 ~1 n: N& A9 U
foreach (DirectoryInfo newDir in directories)0 K& r. s0 l4 _# T9 |
{
/ S2 [9 I6 Z+ Z8 v! V+ M dirCounter++;
]' f6 G& U, V( b& @ ExploreDirectory(newDir);
8 V/ }7 h5 ?+ L2 l7 e }</P>
1 a2 X* t N: ^1 I7 ?3 i) n< > indentLevel--;0 I4 G V6 J: j
}</P>
( M! D4 u# G# U/ a% V( b" @< > private static int dirCounter = 1;//计数目录) q( E# x4 @$ i; z+ l# {
private static int indentLevel = -1;//目录进缩
' A6 h# z( v* t% n4 W# }// private static int fileCounter = 0;//计数文件0 Z3 y2 {& L. z1 y" H* [
}
. h# Q+ z" A. [& C}</P> |
|