A simple library to generate folders and files from string properties containing paths.
public class Data
{
[Path] public string FilePath { get; init; } = "file.txt";
// Don't forget that directories must end with a slash, otherwise it is a file without an extension
[Path] public string FolderPath { get; init; } = "Folder/AnotherFolder/";
}This attribute contains two parameters: bool addRootFolter = true and FileOptions fileOptions = FileOptions.None.
The first one is used to modify the value of the property with the attribute if this requires merging with a root folder.
For instance, if there is a root folder "C:/RootFolder/", it modifies the FilePath value from
"file.txt" to "C:/RootFolder/file.txt". Later you will learn how to configure the root directory.
The second parameter is just a bunch of options for creating a file using File.Create()
// ...
Data data = new Data()
{
FilePath = "file.txt",
FolderPath = "Folder/AnotherFolder/"
};
IPathsResolver pathsResolver = new PathsResolver();
pathsResolver.Resolve(data);
// ...As a result, "file.txt" and "Folder/AnotherFolder/" will be generated in the main directory of the program.
You can set up your own root folder in the PathsResolver constructor as in the example below:
Data data = new Data()
{
FilePath = "file.txt",
FolderPath = "Folder/AnotherFolder/"
};
IPathsResolver pathsResolver = new PathsResolver(Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "PathsResolverTest"));
pathsResolver.Resolve(data);
Console.WriteLine(data.FilePath);
Console.WriteLine(data.FolderPath);
// Output:
// "C:\\ProgramData\\PathsResolverTest\\file.txt"
// "C:\\ProgramData\\PathsResolverTest\\Folder\\AnotherFolder\\"