Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Shine.Models.Scanners
- {
- using System;
- using System.Collections.Generic;
- using System.IO;
- public class Scanner
- {
- private string filePath = @"folders.txt";
- private string movieFolderPath = @"D:\Test\Movies";
- public void ScanDirectory()
- {
- if (!this.CheckIfFileExists())
- {
- this.WriteFoldersToDatabase();
- }
- else
- {
- var oldDirectories = this.GetDirectoriesFromDatabase();
- var newDirectories = this.GetDirectoris();
- foreach (var newDir in newDirectories)
- {
- if (!oldDirectories.Contains(newDir))
- {
- Console.WriteLine("New Folder was created: {0}", newDir);
- }
- }
- foreach (var oldDir in oldDirectories)
- {
- if (!newDirectories.Contains(oldDir))
- {
- Console.WriteLine("A folder was deleted: {0}", oldDir);
- }
- }
- this.WriteFoldersToDatabase();
- }
- }
- private bool CheckIfFileExists()
- {
- bool result = File.Exists(this.filePath);
- return result;
- }
- private HashSet<string> GetDirectoris()
- {
- var directories = Directory.GetDirectories(this.movieFolderPath);
- HashSet<string> directoryList = new HashSet<string>();
- foreach (var dir in directories)
- {
- var directoryInfo = new DirectoryInfo(dir);
- directoryList.Add(directoryInfo.Name);
- }
- return directoryList;
- }
- private HashSet<string> GetDirectoriesFromDatabase()
- {
- HashSet<string> directories = new HashSet<string>();
- var result = File.ReadAllLines(this.filePath);
- foreach (var dir in result)
- {
- directories.Add(dir);
- }
- return directories;
- }
- private void WriteFoldersToDatabase()
- {
- var directories = this.GetDirectoris();
- File.WriteAllLines(this.filePath, directories);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement