The DocumentEngine class

The purpose of this class is merely to provide supporting code to a document. In the eBookManager application, I am going to use a single method called GetFileProperties() that will (you guessed it) return the properties of a selected file. This class also only contains this single method. As the application is modified for your specific purposes, you can modify this class and add additional methods specific to documents.

The DocumentEngine class introduces us to the next feature of C# 7 called tuples. What do tuples do exactly? It is often a requirement for a developer to return more than a single value from a method. Among other Solutions, you can of course use out parameters, but these do not work in async methods. Tuples provide a better way to do this.

Inside the DocumentEngine class, add the following code:

public (DateTime dateCreated, DateTime dateLastAccessed, string fileName, string fileExtension, long fileLength, bool error) GetFileProperties(string filePath) 
{ 
    var returnTuple = (created: DateTime.MinValue,
lastDateAccessed: DateTime.MinValue, name: "", ext: "",
fileSize: 0L, error: false); try { FileInfo fi = new FileInfo(filePath); fi.Refresh(); returnTuple = (fi.CreationTime, fi.LastAccessTime, fi.Name,
fi.Extension, fi.Length, false); } catch { returnTuple.error = true; } return returnTuple; }

The GetFileProperties() method returns a tuple as (DateTime dateCreated, DateTime dateLastAccessed, string fileName, string fileExtension, long fileLength, bool error) and allows us to inspect the values returned from the calling code easily.

Before I try to get the properties of the specific file, I initialize the tuple by doing the following:

var returnTuple = (created: DateTime.MinValue, lastDateAccessed: DateTime.MinValue, name: "", ext: "", fileSize: 0L, error: false); 

If there is an exception, I can return default values. Reading the file properties is simple enough using the FileInfo class. I can then assign the file properties to the tuple by doing this:

returnTuple = (fi.CreationTime, fi.LastAccessTime, fi.Name, fi.Extension, fi.Length, false); 

The tuple is then returned to the calling code where it will be used as required. We will have a look at the calling code next.