⚠ Deprecated: Other
As I learned more about using the IHostBuilder/IHost API, I came to realize almost all of what I'd implemented in this assembly could be handled in the IHostBuilder/IHost system. Consequently, I'm deprecating my own assembly. J4JSoftware.DependencyInjection also contained an API for locating files. I've moved this to a new assembly, J4JSoftware.FileUtilities.
Suggested alternative: J4JSoftware.FileUtilities
provides customized IHostBuilder and IHost classes for simplified configuration
$ dotnet add package J4JSoftware.DependencyInjection| API | Description |
|---|---|
| J4JHostConfiguration | An extended/enhanced version of the IHost interface. It is incorporated into my J4JDeusEx API to provide a ViewModelLocator capability for most common Windows runtime environments (e.g., console, WPF, Windows Applications aka WinApp3) |
| FileUtilities | Provides a static method for searching for and validating files |
| FileLocator | Declarative API for locating files experimental |
The J4JHostConfiguration API allows you to create objects implementing the IJ4JHost interface. That interface extends Microsoft's IHost interface to include:
StringComparison object used to compare text entered on the command line to programmatic values;The IJ4JHost instance provides a number of useful services:
Microsoft.AspNetCore.DataProtection)This assembly targets Net 7 and has nullability enabled.
The repository is available online at github.
Sandboxed refers to whether or not the app has, potentially, unfettered access to the filesystem. Modern desktop apps (e.g., Windows Application v3 aka WinApp3, and, I think, UWP) are sandboxed, at least so far as storing application data is concerned. Older desktop environments (e.g., Windows Forms, WPF) are not sandboxed.
FileExtensions.ValidateFilePath validates the existence and, optionally, writeability, of a file. It can search specified alternative folders for the file if it is not found on the original path that's provided. It also supports logging via my ILogger system. All log events are set at the Verbose level.
public static bool ValidateFilePath(
string path,
out string? result,
string? reqdExtension = ".json",
IEnumerable<string>? folders = null,
bool requireWriteAccess = false,
ILogger? logger = null
)| Argument | Description |
|---|---|
path | the file path to check |
result | the path to the file if it was found, or null if it was not |
reqdExtension | the required file extension (optional; default = .json). If specified, path will be forced to match it. |
folders | a list of folders to search for the file if it isn't found at path (optional). See below for details. |
requireWriteAccess | require that path be writeable. This is verified by testing to see if a temporary file can be written to the folder where the file was found (the temporary file is deleted after the test). |
logger | an instance of ILogger to record log events (optional). All log events are marked at the Verbose level |
ValidateFilePath looks for the required file as follows:
path is not required and path exists, result is set to path and true is returnedpath exists and is writeable, result is set to path and true is returnedpath is not rooted (i.e., it's relative), the specified folders are searched using the relative path. If a match is found and writeability matches what was specified, result is set to the matching path and true is returnedpath was not found, or if path is rooted (i.e., it's absolute), the specified folders are searched for the file name component of path alone. If a match is found and writeability matches what was specified, result is set to the matching path and true is returnedresult is set to null and false is returned.