Desktop Folder Manager in cross platform C# for handling applications storage of user data and user configuration data.
The system enables creation of a subfolder structure with .../YourCompany/YourApp/ ... where you can store data and or configuration files.
On Windows the path could be:
C:\Users\john\AppData\Roaming\YourCompany\YourApp
On MacOS the path could be:
/Users/john/.config/YourCompany/YourApp
Include the namespace:
using MiJenner.ConfigUtils;Next, use the configuration builder to build a configuration object:
var config = new DesktopFolderManagerConfigBuilder()
.WithUserDataPolicy(UserDataPolicy.PolicyFileAppDataRoaming)
.WithUserDataMagic("")
.WithUserConfigPolicy(UserConfigPolicy.PolicyFileAppDataRoaming)
.WithUserConfigMagic("")
.WithCompanyAndAppName("YourCompany", "YourApp")
.Build();UserDataPolicy and UserConfigPolicy choose from the same list of the following enums / locations (if username is john):
PolicyFileAppDataLocal: Windows:C:\Users\john\AppData\Local, MacOS:/Users/john/.local/share, Linux: todoPolicyFileAppDataRoaming: Windows:C:\Users\john\AppData\Roaming, MacOS:/Users/john/.config, Linux: todoPolicyFileDesktop: Windows:C:\Users\john\Desktop, MacOS:/Users/john/Desktop, Linux: todoPolicyFileDocument: Windows:C:\Users\john\Documents, MacOS:/Users/john/Documents, Linux: todoPolicyFileUserProfile: Windows:C:\Users\john, MacOS:/Users/john, Linux: todoPolicyFileTempPath: Windows:C:\Users\john\AppData\Local\Temp, MacOS:/var/folders/..., Linux:/tmp
Note:
PolicyFileDocumentandPolicyFileAppDataRoamingmay not be accessible due to IT security restrictions, for example in school or corporate environments. In those cases, tryPolicyFileUserProfileas a fallback, orPolicyFileTempPathas a last resort. Be aware that the temp folder may be cleared on logoff.
Next, you will typically create a FolderManager instance, based on the just created configuration:
var folderManager = new DesktopFolderManager(config);Note: The constructor will throw
ArgumentExceptionifCompanyNameorAppNameis null or whitespace, andArgumentNullExceptionifconfigis null.
And you would use some logic to determine proper folders for users data and for users configuration, which may be the same. And once found, you want to create them (if already existing this will not harm content). For user data this could be something like:
string dataFolder;
if (!folderManager.TryGetDataFolderPath(out dataFolder))
{
// user data folder path could not be determined.
// logic to get out of that situation.
}
else
{
// try to create user data folder:
if (!folderManager.TryCreateUserDataFolder())
{
// user data folder could not be created.
// logic to get out of that situation.
}
}And if it differs for user configuration data you could use:
string configFolder;
if (!folderManager.TryGetConfigFolderPath(out configFolder))
{
// configuration folder path could not be determined.
// logic to get out of that situation.
}
else
{
// try to create user configuration folder:
if (!folderManager.TryCreateUserConfigFolder())
{
// user configuration folder could not be created.
// logic to get out of that situation.
}
}Note:
TryCreateUserDataFolder()andTryCreateUserConfigFolder()may throwInvalidOperationExceptionif magic file validation is enabled and the expected magic file is not found. This indicates a serious configuration problem that should be handled at application level.
Now your strings dataFolder and configFolder are (hopefully) proper paths to where you want to store user data, sqlite file, json files etc and so on.
There is a NuGet package available for easy usage.