Life's random bits By b1thunt3r (aka Ishan Jain)…
Get the User Directory in C#

Get the User Directory in C#

Ishan jain
Sometimes you just need to save some files to the user directory, and the path needs to be cross-platform.

You can get cross-platform user directory by using

Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile)

Possible usage:

var userDir = new DirectoryInfo(Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile));

If you need to get the platform specific directories you can use:

Window %userdir%:

Environment.GetEnvironmentVariable("userdir")

POSIX $HOME (Linux and macOS):

Environment.GetEnvironmentVariable("HOME")