2012-09-08 21 views
50

मैं उस निर्देशिका के निर्देशिका और उपनिर्देशिका में निहित प्रत्येक फ़ाइल और निर्देशिका को सूचीबद्ध करना चाहता हूं। यदि मैंने सी: \ को निर्देशिका के रूप में चुना है, तो प्रोग्राम को उस हार्ड ड्राइव पर प्रत्येक फ़ाइल और फ़ोल्डर का हर नाम मिलेगा जिस पर इसका उपयोग था।निर्देशिका में सभी फ़ाइलों और निर्देशिकाओं को सूचीबद्ध करें + उपनिर्देशिका

एक सूची दिखाई दे सकता है की तरह

 
fd\1.txt 
fd\2.txt 
fd\a\ 
fd\b\ 
fd\a\1.txt 
fd\a\2.txt 
fd\a\a\ 
fd\a\b\ 
fd\b\1.txt 
fd\b\2.txt 
fd\b\a 
fd\b\b 
fd\a\a\1.txt 
fd\a\a\a\ 
fd\a\b\1.txt 
fd\a\b\a 
fd\b\a\1.txt 
fd\b\a\a\ 
fd\b\b\1.txt 
fd\b\b\a 
+0

ब्राउज [वर्ग] (http://msdn.microsoft.com/en-us/library/wa70yfe2 (V के लिए System.IO नाम = बनाम 100)) और [विधियों] (http://msdn.microsoft.com/en-us/library/dd383459 (v = vs.100)) जो आपकी मदद कर सकता है। – Lucero

+0

[यह प्रश्न] देखें (http://stackoverflow.com/q/1651869/335858), और उस हिस्से को छोड़ दें जहां वह पैटर्न से मेल खाता है। – dasblinkenlight

+0

संभावित डुप्लिकेट [सी # में निर्देशिका में सभी फ़ाइलों को दोबारा कैसे सूचीबद्ध करें?] (Http://stackoverflow.com/questions/929276/how-to-recursively-list-all-the-files-in-a- निर्देशिका-इन-सी) – JoshRivers

उत्तर

13

फ़ोल्डर और फ़ाइलें प्राप्त करने के लिए GetDirectories और GetFiles विधियों का उपयोग करें।

सबफ़ोल्डर में फ़ोल्डर और फ़ाइलों को प्राप्त करने के लिए SearchOptionAllDirectories का उपयोग करें।

+0

नाम के बाएं भाग को काटने के लिए [सबस्ट्रिंग] (http://msdn.microsoft.com/en-us/library/hxthx5h6.aspx) का उपयोग करें। :) – Lucero

+0

@ लुसेरो आप कैसे और क्यों करेंगे? 'पथ' अधिक विश्वसनीय तरीकों की पेशकश करता है। – Gusdor

+0

@Gusdor पथ के एक निश्चित बाएं हिस्से को हटाने के लिए 'पथ' का उपयोग करके एक और उपयुक्त तरीका सुझाए जाने के लिए स्वतंत्र महसूस करें, उदा। दिए गए उदाहरण में 'सी: \'। – Lucero

111
string[] allfiles = System.IO.Directory.GetFiles("path/to/dir", "*.*", System.IO.SearchOption.AllDirectories); 

जहां *.* पैटर्न फ़ाइलों

मैच के लिए निर्देशिका की भी जरूरत है, तो आप इस तरह जा सकते हैं:

foreach (var file in allfiles){ 
    FileInfo info = new FileInfo(file); 
// Do something with the Folder or just add them to a list via nameoflist.add(); 
} 
+0

वास्तव में काम नहीं करेगा ... 'Lsit <>' कक्षा? GetFiles वापस क्या करता है? और निर्देशिका नामों के बारे में क्या अनुरोध किया गया था? – Lucero

+1

'GetFiles' विधि एक स्ट्रिंग सरणी देता है। – Guffa

+0

वास्तविक ... आप सही हैं ... मैं 2 दिन पहले क्यूटी के बारे में सीख रहा हूं और थोड़ा सा –

3

मुझे डर लग रहा, GetFiles विधि फ़ाइलें लेकिन नहीं की सूची देता है निर्देशिकाएं प्रश्न में दी गई सूची मुझे संकेत देती है कि परिणाम में फ़ोल्डर भी शामिल होना चाहिए। यदि आप अधिक अनुकूलित सूची चाहते हैं, तो आप GetFiles और GetDirectories को दोबारा कॉल करने का प्रयास कर सकते हैं। इस प्रयास करें:

List<string> AllFiles = new List<string>(); 
void ParsePath(string path) 
{ 
    string[] SubDirs = Directory.GetDirectories(path); 
    AllFiles.AddRange(SubDirs); 
    AllFiles.AddRange(Directory.GetFiles(path)); 
    foreach (string subdir in SubDirs) 
     ParsePath(subdir); 
} 

युक्ति: यदि आप किसी भी विशिष्ट विशेषता जांच करने की जरूरत है, तो आप FileInfo और DirectoryInfo कक्षाओं का उपयोग कर सकते हैं।

17

Directory.GetFileSystemEntries नेट 4.0+ में मौजूद है और दोनों फ़ाइलों और निर्देशिकाओं देता है। यह इतना की तरह कॉल करें:

string[] entries = Directory.GetFileSystemEntries(
    path, "*", SearchOption.AllDirectories); 

ध्यान दें कि यह उपनिर्देशिका है कि आप (UnauthorizedAccessException) करने के लिए पहुँच नहीं है की सामग्री को सूचीबद्ध करने के लिए प्रयास करता है के साथ सामना नहीं होगा, लेकिन यह आपकी आवश्यकताओं के लिए पर्याप्त हो सकता है।

+2

यह अब तक का सबसे अच्छा जवाब है। यह कोड की एक पंक्ति में सभी फाइलें और फ़ोल्डर्स प्राप्त करता है, जो कि अन्य कोई भी नहीं करता है। –

-1
using System.IO; 
using System.Text; 
string[] filePaths = Directory.GetFiles(@"path", "*.*", SearchOption.AllDirectories); 
+0

आपका उत्तर पहले से मौजूद किसी भी शीर्ष-वोट वाले उत्तर में कुछ भी नया नहीं जोड़ता है। –

+1

यह भी गलत है, क्योंकि यह किसी भी निर्देशिका (जैसा कि निर्दिष्ट प्रश्न है), केवल वास्तविक फ़ाइलों को वापस नहीं करता है। –

3
public static void DirectorySearch(string dir) 
    { 
     try 
     { 
      foreach (string f in Directory.GetFiles(dir)) 
      { 
       Console.WriteLine(Path.GetFileName(f)); 
      } 
      foreach (string d in Directory.GetDirectories(dir)) 
      { 
       Console.WriteLine(Path.GetFileName(d)); 
       DirectorySearch(d); 
      } 

     } 
     catch (System.Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
    } 
+1

यदि आप कोड के बारे में थोड़ा सा स्पष्टीकरण जोड़ सकते हैं तो यह आपके उत्तर में सुधार करेगा। – Alex

0

आप निर्देशिका वृक्ष के अंदर एक सबफ़ोल्डर के लिए पहुँच नहीं है, तो Directory.GetFiles बंद हो जाता है और अपवाद प्राप्त स्ट्रिंग [] में एक शून्य मान जिसका परिणाम फेंकता है।

यहाँ, इस उत्तर https://stackoverflow.com/a/38959208/6310707

यह पाश अंदर अपवाद का प्रबंधन करता है देख सकते हैं और संपूर्ण फ़ोल्डर तक काम कर चल रहा है पर रहते हैं।

0

निम्न उदाहरण सबसे तेजी से (parallelized नहीं) जिस तरह से सूची फ़ाइलें और एक निर्देशिका वृक्ष से निपटने अपवादों के मामले में उप फ़ोल्डरों। सभी निर्देशिकाओं को गिनने के लिए SearchOption.AllDirectories का उपयोग करके Directory.EnumerateDirectories का उपयोग करना तेज़ होगा, लेकिन अगर यह अनधिकृत एक्सेस अपवाद या पथटूलॉन्ग अपवाद को हिट करता है तो यह विधि विफल हो जाएगी।

सामान्य ढेर संग्रह प्रकार है, जो एक अंतिम पहले बाहर में (LIFO) ढेर है और प्रत्यावर्तन का उपयोग नहीं करता उपयोग करता है। https://msdn.microsoft.com/en-us/library/bb513869.aspx से, आप सभी उप-निर्देशिकाओं और फ़ाइलों को गिनने और उन अपवादों के साथ प्रभावी ढंग से निपटने की अनुमति देता है।

public class StackBasedIteration 
{ 
    static void Main(string[] args) 
    { 
     // Specify the starting folder on the command line, or in 
     // Visual Studio in the Project > Properties > Debug pane. 
     TraverseTree(args[0]); 

     Console.WriteLine("Press any key"); 
     Console.ReadKey(); 
    } 

    public static void TraverseTree(string root) 
    { 
     // Data structure to hold names of subfolders to be 
     // examined for files. 
     Stack<string> dirs = new Stack<string>(20); 

     if (!System.IO.Directory.Exists(root)) 
     { 
      throw new ArgumentException(); 
     } 
     dirs.Push(root); 

     while (dirs.Count > 0) 
     { 
      string currentDir = dirs.Pop(); 
      string[] subDirs; 
      try 
      { 
       subDirs = System.IO.Directory.EnumerateDirectories(currentDir); //TopDirectoryOnly 
      } 
      // An UnauthorizedAccessException exception will be thrown if we do not have 
      // discovery permission on a folder or file. It may or may not be acceptable 
      // to ignore the exception and continue enumerating the remaining files and 
      // folders. It is also possible (but unlikely) that a DirectoryNotFound exception 
      // will be raised. This will happen if currentDir has been deleted by 
      // another application or thread after our call to Directory.Exists. The 
      // choice of which exceptions to catch depends entirely on the specific task 
      // you are intending to perform and also on how much you know with certainty 
      // about the systems on which this code will run. 
      catch (UnauthorizedAccessException e) 
      {      
       Console.WriteLine(e.Message); 
       continue; 
      } 
      catch (System.IO.DirectoryNotFoundException e) 
      { 
       Console.WriteLine(e.Message); 
       continue; 
      } 

      string[] files = null; 
      try 
      { 
       files = System.IO.Directory.EnumerateFiles(currentDir); 
      } 

      catch (UnauthorizedAccessException e) 
      { 

       Console.WriteLine(e.Message); 
       continue; 
      } 

      catch (System.IO.DirectoryNotFoundException e) 
      { 
       Console.WriteLine(e.Message); 
       continue; 
      } 
      // Perform the required action on each file here. 
      // Modify this block to perform your required task. 
      foreach (string file in files) 
      { 
       try 
       { 
        // Perform whatever action is required in your scenario. 
        System.IO.FileInfo fi = new System.IO.FileInfo(file); 
        Console.WriteLine("{0}: {1}, {2}", fi.Name, fi.Length, fi.CreationTime); 
       } 
       catch (System.IO.FileNotFoundException e) 
       { 
        // If file was deleted by a separate application 
        // or thread since the call to TraverseTree() 
        // then just continue. 
        Console.WriteLine(e.Message); 
        continue; 
       } 
       catch (UnauthorizedAccessException e) 
       {      
        Console.WriteLine(e.Message); 
        continue; 
       } 
      } 

      // Push the subdirectories onto the stack for traversal. 
      // This could also be done before handing the files. 
      foreach (string str in subDirs) 
       dirs.Push(str); 
     } 
    } 
} 
+0

बड़ी संख्या में बड़ी संख्या में फाइलों और निर्देशिकाओं के लिए *** कार्य *** का उपयोग करना? –

+0

https://msdn.microsoft.com/en-us/library/ff477033(v=vs.110).aspx उपरोक्त समाधान का समांतर थ्रेडिंग संस्करण एक स्टैक संग्रह और तेज़ का उपयोग कर है। – Markus

0

तार्किक और आदेश दिया रास्ता:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Reflection; 

namespace DirLister 
{ 
class Program 
{ 
    public static void Main(string[] args) 
    { 
     //with reflection I get the directory from where this program is running, thus listing all files from there and all subdirectories 
     string[] st = FindFileDir(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)); 
     using (StreamWriter sw = new StreamWriter("listing.txt", false)) 
     { 
      foreach(string s in st) 
      { 
       //I write what I found in a text file 
       sw.WriteLine(s); 
      } 
     } 
    } 

    private static string[] FindFileDir(string beginpath) 
    { 
     List<string> findlist = new List<string>(); 

     /* I begin a recursion, following the order: 
     * - Insert all the files in the current directory with the recursion 
     * - Insert all subdirectories in the list and rebegin the recursion from there until the end 
     */ 
     RecurseFind(beginpath, findlist); 

     return findlist.ToArray(); 
    } 

    private static void RecurseFind(string path, List<string> list) 
    { 
     string[] fl = Directory.GetFiles(path); 
     string[] dl = Directory.GetDirectories(path); 
     if (fl.Length>0 || dl.Length>0) 
     { 
      //I begin with the files, and store all of them in the list 
      foreach(string s in fl) 
       list.Add(s); 
      //I then add the directory and recurse that directory, the process will repeat until there are no more files and directories to recurse 
      foreach(string s in dl) 
      { 
       list.Add(s); 
       RecurseFind(s, list); 
      } 
     } 
    } 
} 
} 
+0

क्या आप एक स्पष्टीकरण या ऑनलाइन टिप्पणियां प्रदान कर सकते हैं, आपका कोड क्या करता है? – MarthyM

+0

बेशक, इसे किया, लेकिन यह स्वयं व्याख्यात्मक होना चाहिए, यह सभी निर्देशिकाओं और फ़ाइलों के माध्यम से एक साधारण लूपिंग रिकर्सन है – Sascha

0

मैं एक फार्म 2 बटन, निकास के लिए एक और शुरू करने के लिए अन्य है के साथ निम्नलिखित कोड का उपयोग करें। एक फ़ोल्डर ब्राउज़र संवाद और एक सहेजें फ़ाइल संवाद। कोड के नीचे सूचीबद्ध और अपने सिस्टम Windows10 (64) पर काम करता है:

using System; 
using System.IO; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace Directory_List 
{ 

    public partial class Form1 : Form 
    { 
     public string MyPath = ""; 
     public string MyFileName = ""; 
     public string str = ""; 

     public Form1() 
     { 
      InitializeComponent(); 
     }  
     private void cmdQuit_Click(object sender, EventArgs e) 
     { 
      Application.Exit(); 
     }  
     private void cmdGetDirectory_Click(object sender, EventArgs e) 
     { 
      folderBrowserDialog1.ShowDialog(); 
      MyPath = folderBrowserDialog1.SelectedPath;  
      saveFileDialog1.ShowDialog(); 
      MyFileName = saveFileDialog1.FileName;  
      str = "Folder = " + MyPath + "\r\n\r\n\r\n";  
      DirectorySearch(MyPath);  
      var result = MessageBox.Show("Directory saved to Disk!", "", MessageBoxButtons.OK); 
       Application.Exit();  
     }  
     public void DirectorySearch(string dir) 
     { 
       try 
      { 
       foreach (string f in Directory.GetFiles(dir)) 
       { 
        str = str + dir + "\\" + (Path.GetFileName(f)) + "\r\n"; 
       }  
       foreach (string d in Directory.GetDirectories(dir, "*")) 
       { 

        DirectorySearch(d); 
       } 
         System.IO.File.WriteAllText(MyFileName, str); 

      } 
      catch (System.Exception ex) 
      { 
       Console.WriteLine(ex.Message); 
      } 
     } 
    } 
} 
संबंधित मुद्दे

 संबंधित मुद्दे