❔ How to deal with paths and file system files in a Win/Linux cross platform NET 6 application?
I am working with a cross-platform application (Linux & Windows) that requires a plethora of configuration files and performs a lot of file IO during execution in locations typically under the installation folder. I would like to know, at a high level, how the configuration files, the paths utilized in the code (some hardcoded & some dynamically created), and permissions are best to be maintained in the product code. This is an application that just now is implementing Linux support.
A pain point is that the installation & configuration locations in Windows are different than those on Linux. My thinking is that I would like to have these paths configured somewhere as strings, but load the correct set based on the platform at runtime. However, I do not know whether these is a configuration that facilitates this setup in .NET or whether this is an effective approach.
I am looking for high level recommendations and suggestions on how to deal with file system artifacts as current pain points include:
1. Errors while creating/reading files due to permissions not being set correctly on the files or folders created at runtime
2. Bogus file system folders and files being created when a file path contains spaces and subsequent errors while opening them
3. Subprocesses not starting correctly due to location resolution errors
10 Replies
https://learn.microsoft.com/en-us/dotnet/api/system.environment.getfolderpath?view=net-7.0
and
https://learn.microsoft.com/en-us/dotnet/api/system.environment.specialfolder?view=net-7.0
Environment.GetFolderPath Method (System)
Gets the path to the system special folder that is identified by the specified enumeration.
Environment.SpecialFolder Enum (System)
Specifies enumerated constants used to retrieve directory paths to system special folders.
To further build up paths you can just use
Path.Combine()
, .NET will handle which platform's path-syntax to use
In regards to cross-platform accessibility for certain system pathsThere's one catch. The hundreds of configuration files that are built into the system contain paths that are Windows specific. E.g. C:\This white space\Path\Is\Annoying. Is there something clever I can do to update these files such that the paths here can be used in both environments. Maybe I can use a sort of relative file path for the configuration and append that to platform specific locations, but not sure this is the way to go.
Time to migrate to a different path? 👀
Add a check if old path is still used, copy things over
Just dropping my two cents of thoughts
It sounds like this is inherently messy... which is what I expected...
I mean, it's either dual wielding two platform paths and configurations, or just adding an if statement with a File.Copy()
<:DFrido_Hmm:704598915080781855>
@OneWholesomeDev But in terms of maintaining sets of application-level configuration paths that are platform specific but accessing them through the same group of symbols, think almost like a Static class that resolves its paths at runtime. So I could invoke (for the purpose of this example) MyClass.LogsFolder anywhere this is used in the code and on Windows it would give me something like C:\User\AppData\MyApp\Logs and on Linux \usr\local\MyApp\Logs. You think this is a good idea?
that sounds good
though note that on Linux logs go in
/var/log
(or the systemd journal, if it's a service)
*system-wide logsYeah, sounds good
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.