public void MapDrive(string drive) { ThrowIfNotInitialized(); try { logger.DebugFormat("{0}mapping drive: {1}", Environment.NewLine, drive); DriveInfo di = new DriveInfo(drive + ":\\"); var root = di.RootDirectory; string[] directories = Directory.GetDirectories(root.ToString()); MapFiles mfs = GetMapFiles(directories); for (int i = 0; i < directories.Count(); i++) { try { var directory = directories[i]; var dirs = Directory.GetDirectories(directory); var files = Directory.GetFiles(directory); var dinfo = new DirectoryInfo(directory); foreach (var file in files) { FileInfo info = new FileInfo(file); MapFile mf = new MapFile().LoadData(info); string json = JsonConvert.SerializeObject(mf); } } catch (Exception ex) { logger.ErrorFormat("Unable to process {1}{0}drive: {2}{0}Error: {3}" , Environment.NewLine , GetThisMethodName() , drive , ex.Message); } } } catch (Exception ex) { logger.ErrorFormat("Unable to process {1}{0}drive: {2}{0}Error: {3}" , Environment.NewLine , GetThisMethodName() , drive , ex.Message); } }
private MapFiles WalkDirectoryTree(System.IO.DirectoryInfo root) { logger.DebugFormat("{0}mapping directory: {1}", Environment.NewLine, root.FullName); MapFiles mfs = new MapFiles(); try { System.IO.FileInfo[] files = null; System.IO.DirectoryInfo[] subDirs = null; // First, process all the files directly under this folder try { files = root.GetFiles("*.*"); } #region catch errors // This is thrown if even one of the files requires permissions greater // than the application provides. catch (UnauthorizedAccessException ex) { // This code just writes out the message and continues to recurse. logger.DebugFormat("Unable to process {1}{0}drive: {2}{0}Error: {3}" , Environment.NewLine , GetThisMethodName() , root.FullName , ex.Message); } catch (System.IO.DirectoryNotFoundException ex) { logger.DebugFormat("Unable to process {1}{0}drive: {2}{0}Error: {3}" , Environment.NewLine , GetThisMethodName() , root.FullName , ex.Message); } catch (System.IO.PathTooLongException ex) { // This code just writes out the message and continues to recurse. logger.DebugFormat("Unable to process {1}{0}drive: {2}{0}Error: {3}" , Environment.NewLine , GetThisMethodName() , root.Name , ex.Message); //Console.ReadKey(); } catch (Exception ex) { // This code just writes out the message and continues to recurse. logger.DebugFormat("Unable to process {1}{0}drive: {2}{0}Error: {3}" , Environment.NewLine , GetThisMethodName() , root.FullName , ex.Message); // Console.ReadKey(); } #endregion catch errors if (files != null) { foreach (System.IO.FileInfo fi in files) { try { MapFile mf = new MapFile().LoadData(fi); string json = JsonConvert.SerializeObject(mf); mfs.Add(mf); } #region catch errors catch (System.IO.PathTooLongException ex) { // This code just writes out the message and continues to recurse. logger.DebugFormat("Unable to process {1}{0}drive: {2}{0}Error: {3}" , Environment.NewLine , GetThisMethodName() , fi.Name , ex.Message); //Console.ReadKey(); } catch (Exception ex) { // This code just writes out the message and continues to recurse. logger.DebugFormat("Unable to process {1}{0}drive: {2}{0}Error: {3}" , Environment.NewLine , GetThisMethodName() , fi.Name , ex.Message); // Console.ReadKey(); } #endregion catch errors } // Now find all the subdirectories under this directory. subDirs = root.GetDirectories(); foreach (System.IO.DirectoryInfo dirInfo in subDirs) { // Resursive call for each subdirectory. var more = WalkDirectoryTree(dirInfo); mfs.AddRange(more); } } } catch (Exception ex) { logger.DebugFormat("Unable to process {1}{0}root: {2}{0}Error: {3}" , Environment.NewLine , GetThisMethodName() , root.FullName , ex.Message); } using (BulkLoadFiles blc = new BulkLoadFiles(logger)) { var dtc = blc.ConfigureDataTable(); dtc = blc.LoadDataTableWithFiles(mfs, dtc); blc.BulkCopy<MapFiles>(dtc, "DCMaperContext"); } return mfs; }