Mega Code Archive

 
Categories / C# / File Stream
 

Find all files in a directory, and all files within every nested directory (2)

// SilverlightBridge // Copyright (c) 2009 // by OpenLight Group // http://openlightgroup.net/ // // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated  // documentation files (the "Software"), to deal in the Software without restriction, including without limitation  // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and  // to permit persons to whom the Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all copies or substantial portions  // of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED  // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL  // // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER  // DEALINGS IN THE SOFTWARE. using System; using System.Data; using System.Linq; using System.Web; using System.Xml.Linq; using System.Net.Mail; using System.Text; using System.Collections.Generic; using System.IO; namespace SilverlightFileManager {     public class Utility     {         #region GetAllFileNames         /// <summary>         /// Find all files in a directory, and all files within every nested         /// directory.         /// from: http://dotnetperls.com/Content/Recursively-Find-Files.aspx         /// </summary>         /// <param name="baseDir">The starting directory you want to use.</param>         /// <returns>A string array containing all the file names.</returns>         public static string[] GetAllFileNames(string baseDir)         {             // Store results in the file results list.             List<string> fileResults = new List<string>();             // Store a stack of our directories.             Stack<string> directoryStack = new Stack<string>();             directoryStack.Push(baseDir);             // While there are directories to process and we don't have too many results             while (directoryStack.Count > 0 && fileResults.Count < 1000)             {                 string currentDir = directoryStack.Pop();                 // Add all files at this directory.                 foreach (string fileName in Directory.GetFiles(currentDir, "*.*"))                 {                     fileResults.Add(fileName);                 }                 // Add all directories at this directory.                 foreach (string directoryName in Directory.GetDirectories(currentDir))                 {                     directoryStack.Push(directoryName);                 }             }             return fileResults.ToArray();         }         #endregion         #region GetRandomPassword         public string GetRandomPassword()         {             StringBuilder builder = new StringBuilder();             Random random = new Random();             int intElements = random.Next(10, 26);             for (int i = 0; i < intElements; i++)             {                 int intRandomType = random.Next(0, 2);                 if (intRandomType == 1)                 {                     char ch;                     ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));                     builder.Append(ch);                 }                 else                 {                     builder.Append(random.Next(0, 9));                 }             }             return builder.ToString();         }         #endregion         #region EnsureDirectory         public static void EnsureDirectory(System.IO.DirectoryInfo oDirInfo)         {             if (oDirInfo.Parent != null)                 EnsureDirectory(oDirInfo.Parent);             if (!oDirInfo.Exists)             {                 oDirInfo.Create();             }         }         #endregion     } }