WEB开发网      濠电娀娼ч崐濠氬疾椤愶附鍋熸い鏍ㄧ〒闂勫嫰鏌﹀Ο渚Ц闁诲氦顕ч湁婵犲﹤楠告禍鍓х磼鏉堛劌绗氶柟宄版嚇閹晠宕归銈嗘濠电偞鍨堕幐鎾磻閹捐秮褰掓偐閻戞﹩妫勯梺鎼炲妼鐎涒晝绮嬪澶樻晝闁挎繂鏌婇敃鍌涚厵閻庢稒锚閻忥絾绻濇繝鍐ㄧ伌闁诡垰鍟村畷鐔碱敂閸♀晙绱樺┑鐐差嚟婵儳螞閸曨剚鍙忛柍鍝勬噹缁€澶嬬箾閹存繄锛嶆鐐灲閹綊宕惰濡插鏌涢妸銉ヮ劉缂佸倸绉归弫鎾绘晸閿燂拷 ---闂備焦瀵уú鈺呭箯閿燂拷
开发学院WEB开发ASP.NET .Net Framework 4.0: Enumerating file system ob... 阅读

.Net Framework 4.0: Enumerating file system objects

 2009-10-28 16:51:09 来源:WEB开发网 闂備線娼уΛ鎾箯閿燂拷闂備礁鎲¢崹鐢垫崲閹扮増鍎嶆い鎺戝€甸崑鎾斥槈濞嗗秳娌紓鍌氱▌閹凤拷濠电姭鎷冮崨顓濈捕闂侀潧娲ゅú銊╁焵椤掍胶鈯曢柕鍥╁仧缁辩偤鏁撻敓锟�闂備線娼уΛ鎾箯閿燂拷  闂備胶枪缁绘鈻嶉弴銏犳瀬闁绘劕鎼痪褔鏌曟繝蹇曠窗闁煎壊浜滈—鍐偓锝庡墮娴犙勭箾閸喎鐏ユい鏇樺劦椤㈡瑩鎮℃惔銇帮拷
核心提示:2009-10-27 20:07:44 | .net教程聚合 In my last posting I introduced new ReadLines() method and new overloads for WriteAllLines() method of File class. But there are
2009-10-27 20:07:44 | .net教程聚合
In my last posting I introduced new ReadLines() method and new overloads for WriteAllLines() method of File class. But there are more new stuff in System.IO namespace. In .Net Framework 4.0 Directory and DirectoryInfo class are able to enumerate files, directories and file system entries. In this posting I will show you these new features.

Let’s see now how to enumerate file system objects using new static methods of Directory class. There is one thing you should know. If you have to handle files and directories in same context then you should use EnumerateFileSystemEntries() method that enumerates both files and directories. The following example shows you how to enumerate different file system objects.


--------------------------------------------------------------------------------

static void Main(string[] args)

{

  var path = Path.GetPathRoot(Environment.CurrentDirectory);



  var files = Directory.EnumerateFiles(path);
  var directories = Directory.EnumerateDirectories(path);  
  var entries = Directory.EnumerateFileSystemEntries(path);



  Console.WriteLine("Files:");

  foreach (var file in files)

    Console.WriteLine(file);



  Console.WriteLine("\r\nDirectories:");

  foreach (var directory in directories)

    Console.WriteLine(directory);



  Console.WriteLine("\r\nEntries:");

  foreach (var entry in entries)

    Console.WriteLine(entry);



  Console.ReadLine();

}


--------------------------------------------------------------------------------

You should get output like this (the path is root path of drive where application is located).

Files:
C:\autoexec.bat
C:\config.sys
C:\pagefile.sys
 Directories:
C:\$Recycle.BinC:\Documents and Settings
C:\Install
C:\PerfLogs C:\PRogram Files
C:\ProgramData C:\Recovery C:\System Volume Information C:\Users C:\Windows
 Entries:
C:\$Recycle.Bin
C:\autoexec.bat
C:\config.sys
C:\Documents and Settings
C:\Install
C:\pagefile.sys
C:\PerfLogs
C:\Program Files
C:\ProgramData
C:\Recovery
C:\System Volume Information
C:\Users
C:\Windows
These enumerating methods work better than ones that return arrays because requests to file system are made only when concrete object is asked from enumerator. I think these methods may be very good load balancers in applications that make heavy use of file system.

Tags:Net Framework Enumerating

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接