Class MemoryStorageService
This class implements the storage interface for reading and writing to a virtual directory in memory.
Inheritance
Implements
Inherited Members
Namespace: Talegen.Storage.Net.Core.Memory
Assembly: Talegen.Storage.Net.Core.dll
Syntax
public class MemoryStorageService : IStorageService
Remarks
This is useful for faking storage for unit tests within your application.
Constructors
| Improve this Doc View SourceMemoryStorageService(String, Boolean)
Initializes a new instance of the MemoryStorageService class.
Declaration
public MemoryStorageService(string workspacePath, bool uniqueWorkspace = false)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | workspacePath | Contains the root path where the local storage folder and file structure begins. |
| System.Boolean | uniqueWorkspace | Contains a value indicating whether the storage service shall use a unique workspace sub-folder. |
MemoryStorageService(MemoryStorageContext)
Initializes a new instance of the MemoryStorageService class.
Declaration
public MemoryStorageService(MemoryStorageContext storageContext)
Parameters
| Type | Name | Description |
|---|---|---|
| MemoryStorageContext | storageContext | Contains an implementation of the storage context. |
Properties
| Improve this Doc View SourceRootPath
Gets or sets the root folder path for this service instance.
Declaration
public string RootPath { get; set; }
Property Value
| Type | Description |
|---|---|
| System.String |
StorageId
Gets or sets the storage identity for this service instance.
Declaration
public string StorageId { get; }
Property Value
| Type | Description |
|---|---|
| System.String |
VirtualDisk
Gets the virtual disk accessor.
Declaration
public static ConcurrentDictionary<string, MemoryFolderModel> VirtualDisk { get; }
Property Value
| Type | Description |
|---|---|
| System.Collections.Concurrent.ConcurrentDictionary<System.String, MemoryFolderModel> |
Methods
| Improve this Doc View SourceCopyFile(String, String, Boolean)
This method is used to copy a file.
Declaration
public void CopyFile(string sourceFilePath, string targetFilePath, bool overwrite = true)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | sourceFilePath | Contains a the path to the file that will be copied. |
| System.String | targetFilePath | Contains the path to the target where the file is to be copied. |
| System.Boolean | overwrite | Contains a value indicating if the target should be overwritten if it already exists. Default is true. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | sourceFilePath or targetFilePath |
| StorageException |
CreateDirectory(String, Boolean)
This method is used to create a temporary directory within the Inspire application path.
Declaration
public string CreateDirectory(string subFolderName, bool silentExists = false)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | subFolderName | Contains a sub-folder that will be included in the working directory path. |
| System.Boolean | silentExists | Contains a value indicating whether the method is silently return successfully if the folder path already exists. |
Returns
| Type | Description |
|---|---|
| System.String | Returns the name of the directory that was created. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | subFolderName |
| StorageException |
DeleteDirectory(String, Boolean, Boolean)
This method is used to delete a directory and all of its files.
Declaration
public void DeleteDirectory(string subFolderName, bool recursive = true, bool silentNoExist = true)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | subFolderName | Contains the name of the directory that will be deleted. |
| System.Boolean | recursive | Delete all contents within the folder. |
| System.Boolean | silentNoExist | Contains a value indicating whether an exception is thrown if the target folder does not exist. Default is true; no exception is thrown if the folder does not exist. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | subFolderName |
| StorageException | thrown if the specified folder does not exist. |
DeleteFile(String, Boolean)
This method is used to delete a file.
Declaration
public void DeleteFile(string filePath, bool deleteDirectory = false)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | filePath | Contains a the path to the file that will be deleted. |
| System.Boolean | deleteDirectory | Contains a value indicating whether the directory the file is within will be deleted. This will only occur if no other files remain in the directory after the list of files have been deleted. |
DeleteFiles(List<String>, Boolean)
This method is used to delete a list of files.
Declaration
public void DeleteFiles(List<string> filePathNames, bool deleteDirectory = false)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.List<System.String> | filePathNames | Contains a list of file names that will be deleted. |
| System.Boolean | deleteDirectory | Contains a value indicating whether the directory or directories the files are within will be deleted. This will only occur if no other files remain in the directory after the list of files have been deleted. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | filePathNames |
| StorageException |
DirectoryExists(String)
Contains a value indicating whether the folder already exists.
Declaration
public bool DirectoryExists(string subFolderName)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | subFolderName | Contains the sub-folder name excluding the root folder path. |
Returns
| Type | Description |
|---|---|
| System.Boolean | Returns a value indicating whether the directory exists. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | subFolderName |
EmptyDirectory(String)
This method is used to delete all directories and files inside of a sub-folder within the Inspire application data directory.
Declaration
public void EmptyDirectory(string subFolderName)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | subFolderName | Contains the sub-folder within the Inspire application data directory. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | subFolderName |
| StorageException | thrown if the specified folder does not exist. |
FileExists(String)
Contains a value indicating whether the file exists.
Declaration
public bool FileExists(string filePath)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | filePath | Contains the sub-folder path and file name excluding the root folder path to determine if exists. |
Returns
| Type | Description |
|---|---|
| System.Boolean | Returns a value indicating whether the file exists. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | filePath |
FileHash(String)
This method is used to create a hash of the file contents.
Declaration
public string FileHash(string filePath)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | filePath | The path to the file. |
Returns
| Type | Description |
|---|---|
| System.String | Returns a hash of the file contents. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | filePath |
FindFiles(String, String, SearchOption)
This method is used to get the files from a directory.
Declaration
public List<string> FindFiles(string subFolderName, string searchPattern = "*.*", SearchOption searchOption = SearchOption.AllDirectories)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | subFolderName | Contains the sub-folder name to get the files. |
| System.String | searchPattern | Contains an optional file name search pattern. If not specified, . is used. |
| System.IO.SearchOption | searchOption | Contains search options. If not specified, all sub-folders are searched for the file pattern. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.List<System.String> | Returns a list of files in the directory path. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | subFolderName |
Initialize(MemoryStorageContext)
This method is used to initialize a storage service with the specified settings provided within the MemoryStorageContext object.
Declaration
public void Initialize(MemoryStorageContext storageContext)
Parameters
| Type | Name | Description |
|---|---|---|
| MemoryStorageContext | storageContext | Contains the settings used to initialize the storage service. |
MoveFile(String, String, Boolean)
This method is used to move a file.
Declaration
public void MoveFile(string sourceFilePath, string targetFilePath, bool overwrite = true)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | sourceFilePath | Contains a the path to the file that will be moved. |
| System.String | targetFilePath | Contains the path to the target where the file is to be moved. |
| System.Boolean | overwrite | Contains a value indicating if the target should be overwritten if it already exists. Default is true. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | sourceFilePath or targetFilePath |
| StorageException |
ReadBinaryFile(String)
This method is used to read all the bytes from a binary file.
Declaration
public byte[] ReadBinaryFile(string path)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | path | Contains the path to the file to load and return. |
Returns
| Type | Description |
|---|---|
| System.Byte[] | Returns a byte array containing the binary bytes of the target file. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | path |
| StorageException |
ReadBinaryFile(String, Stream)
This method is used to read all the bytes from a binary file to a provided stream.
Declaration
public void ReadBinaryFile(string path, Stream outputStream)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | path | Contains the path to the file to load into the stream. |
| System.IO.Stream | outputStream | The stream to write the file to. |
ReadTextFile(String, Encoding)
This method is used to read all the bytes from a text file.
Declaration
public string ReadTextFile(string path, Encoding encoding = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | path | Contains the path to the file to load and return. |
| System.Text.Encoding | encoding | Contains the text encoding type. If none is specified, Encoding.Default is used. |
Returns
| Type | Description |
|---|---|
| System.String | Returns a string containing the content of the target file. |
WriteBinaryFile(String, Byte[])
This method is used to write content to the specified path.
Declaration
public bool WriteBinaryFile(string path, byte[] content)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | path | Contains the fully qualified path, including file name, to the location in which the binary content shall be written. |
| System.Byte[] | content | Contains a byte array of the content to be written. |
Returns
| Type | Description |
|---|---|
| System.Boolean | Returns a value indicating whether the write was successful. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | path |
| StorageException |
WriteBinaryFile(String, Stream)
This method is used to write content to the specified path.
Declaration
public bool WriteBinaryFile(string path, Stream inputStream)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | path | Contains the fully qualified path, including file name, to the location in which the binary content shall be written. |
| System.IO.Stream | inputStream | Contains a stream of the content to be written. |
Returns
| Type | Description |
|---|---|
| System.Boolean | Returns a value indicating whether the write was successful. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | Thrown if argument is null. |
| System.IO.IOException | Thrown if stream cannot be read. |
WriteTextFile(String, String, Encoding)
This method is used to write content to the specified path.
Declaration
public bool WriteTextFile(string path, string content, Encoding encoding = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | path | Contains the fully qualified path, including file name, to the location in which the text content shall be written. |
| System.String | content | Contains a string of the content to be written. |
| System.Text.Encoding | encoding | Contains the text file encoding. If none specified, Encoding.Default is used. |
Returns
| Type | Description |
|---|---|
| System.Boolean | Returns a value indicating whether the write was successful. |