File System Controller

Create Class

Create a static class called FileSystemController in the Controller namespace

See: Classes

namespace Library.Controller;
public static class FileSystemController
{}

Methods

  • Directory Iterator

  • Scan

  • Print

  • Save Result

  • Save

  • Open File

Directory Iterator

create a public static async method named DirectoryIterator that takes a String as a parameter and returns Task<(ConcurrentDictionary<string, FileModel> Model, int FileCount)>

create a Concurrent Dictionary with a String and a FileModel, Concurrent Dictionaries are Dictionaries that are thread safe.

create an array of all files.

Create a Parallel foreach loop through all file entries, a Parallel foreach is a foreach loop that splits the given array into many smaller arrays and creates a Thread around them, then process's the array. See: Parallel Tasks

Create a Nullable string named extension See: Nullable Variables

Now get the FileModel instance See: Get File

Check if the file data already contains the instance extension and if it does append the instance to it

otherwise create a new entry

Finally iterate the file count

Extra Note: You may want to wrap everything in a try catch

Overview

Scan

create a public static async method named Scan that takes a String as a parameter and returns Task<ResultModel>

Start a stopwatch

Create a variable for model and file count

Stop the stopwatch

Then create and return a ResultModel See: Result Model

Overview

Print

create a public static method named Print that takes a ResultModel as a parameter and returns void

Loop through each item in scanResult

Overview

Save

Create a file stream and a stream writer to write the scanResult

See: Result Model

Overview

Open File

Create a method named OpenFile that takes in a String named file

Next call Process.Start method with ProcessStartInfo with the file name of file and UseShellExecute set to true

Overview

Overview

Last updated

Was this helpful?