Advertisement
murkata86

DirectoryScanner

Aug 30th, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. namespace Shine.Models.Scanners
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.IO;
  6.  
  7.     public class Scanner
  8.     {
  9.         private string filePath = @"folders.txt";
  10.         private string movieFolderPath = @"D:\Test\Movies";
  11.  
  12.         public void ScanDirectory()
  13.         {
  14.             if (!this.CheckIfFileExists())
  15.             {
  16.                 this.WriteFoldersToDatabase();
  17.             }
  18.             else
  19.             {
  20.                 var oldDirectories = this.GetDirectoriesFromDatabase();
  21.                 var newDirectories = this.GetDirectoris();
  22.  
  23.                 foreach (var newDir in newDirectories)
  24.                 {
  25.                     if (!oldDirectories.Contains(newDir))
  26.                     {
  27.                         Console.WriteLine("New Folder was created: {0}", newDir);
  28.                     }
  29.                 }
  30.  
  31.                 foreach (var oldDir in oldDirectories)
  32.                 {
  33.                     if (!newDirectories.Contains(oldDir))
  34.                     {
  35.                         Console.WriteLine("A folder was deleted: {0}", oldDir);
  36.                     }
  37.                 }
  38.  
  39.                 this.WriteFoldersToDatabase();
  40.             }          
  41.         }
  42.  
  43.         private bool CheckIfFileExists()
  44.         {
  45.             bool result = File.Exists(this.filePath);
  46.  
  47.             return result;
  48.         }
  49.  
  50.         private HashSet<string> GetDirectoris()
  51.         {
  52.             var directories = Directory.GetDirectories(this.movieFolderPath);
  53.  
  54.             HashSet<string> directoryList = new HashSet<string>();
  55.  
  56.             foreach (var dir in directories)
  57.             {
  58.                 var directoryInfo = new DirectoryInfo(dir);
  59.  
  60.                 directoryList.Add(directoryInfo.Name);
  61.             }
  62.  
  63.             return directoryList;
  64.         }
  65.  
  66.         private HashSet<string> GetDirectoriesFromDatabase()
  67.         {
  68.             HashSet<string> directories = new HashSet<string>();
  69.  
  70.             var result = File.ReadAllLines(this.filePath);
  71.  
  72.             foreach (var dir in result)
  73.             {
  74.                 directories.Add(dir);
  75.             }
  76.  
  77.             return directories;
  78.         }
  79.  
  80.         private void WriteFoldersToDatabase()
  81.         {
  82.             var directories = this.GetDirectoris();
  83.  
  84.             File.WriteAllLines(this.filePath, directories);
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement