निम्न उदाहरण सबसे तेजी से (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);
}
}
}
ब्राउज [वर्ग] (http://msdn.microsoft.com/en-us/library/wa70yfe2 (V के लिए System.IO नाम = बनाम 100)) और [विधियों] (http://msdn.microsoft.com/en-us/library/dd383459 (v = vs.100)) जो आपकी मदद कर सकता है। – Lucero
[यह प्रश्न] देखें (http://stackoverflow.com/q/1651869/335858), और उस हिस्से को छोड़ दें जहां वह पैटर्न से मेल खाता है। – dasblinkenlight
संभावित डुप्लिकेट [सी # में निर्देशिका में सभी फ़ाइलों को दोबारा कैसे सूचीबद्ध करें?] (Http://stackoverflow.com/questions/929276/how-to-recursively-list-all-the-files-in-a- निर्देशिका-इन-सी) – JoshRivers