Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Locating special folders in cross-platform .NET applications

November 7, 2018
Tom Deseyn
Related topics:
Linux.NET
Related products:
Red Hat Enterprise Linux

    .NET has APIs for locating special folders that can be used for application and user configuration and data storage. They provide a convenient, portable way to make cross-platform applications find the appropriate folders on different operating systems. We'll look at how Environment.GetFolderPath, Path.GetTempPath, and Path.GetTempFileName behave on Linux.

    Environment.GetFolderPath

    The System.Environment class has two GetFolderPath overloads:

    public static string GetFolderPath (SpecialFolder folder);
    public static string GetFolderPath (SpecialFolder folder, SpecialFolderOption option);
    

    SpecialFolder is an enum with values like ApplicationData, MyDocuments, and ProgramFiles. The SpecialFolderOption enum has three values: None, Create, and DoNotVerify. These control the return value when the folder does not exist. Specifying None causes an empty string to be returned. Specifying Create causes the folder to be created. And DoNotVerify causes the path to be returned even when the folder does not exist.

    Note that SpecialFolder and SpecialFolderOption are nested in the Environment class, and to use them directly you should add a using static System.Environment; statement to your code.

    To make this API work cross-platform, .NET Core needs to map the SpecialFolder values to some locations. For Linux, this mapping is based on file-hierarcy and basedir-spec.

     

    SpecialFolder Environment Variable Config File Linux Default
    CommonApplicationData /usr/share
    CommonTemplates /usr/share/templates
    MyDocuments (home) HOME passwd /
    UserProfile home
    ApplicationData XDG_CONFIG_HOME home/.config
    LocalApplicationData XDG_DATA_HOME home/.local/share
    Fonts home/.fonts
    Desktop, DesktopDirectory XDG_DESKTOP_DIR user-dirs.dirs home/Desktop
    Templates XDG_TEMPLATES_DIR user-dirs.dirs home/Templates
    MyVideos XDG_VIDEOS_DIR user-dirs.dirs home/Videos
    MyMusic XDG_MUSIC_DIR user-dirs.dirs home/Music
    MyPictures XDG_PICTURES_DIR user-dirs.dirs home/Pictures

    The table lists all the mapped values in .NET Core 2.1. Other values are not mapped and return an empty string. The returned value is determined from left to right: first checking the environment variable, then falling back to the config file, and finally falling back to a default.

    Cross-platform applications should be limited to using the mapped values, or they should be able to fall back to another location when GetFolderPath returns an empty string.

    The user home folder is read from the HOME environment variable. When that is unset, the home directory is read from the system user database. It’s safe to assume that for known users, .NET Core will be able to determine the home directory. A number of other locations are based on the user home folder. Some can be overridden using environment variables and some others by using a file at ApplicationData/users-dirs.dirs.

    On Windows, most of the special folders will exist by default. This may not be the case on Linux. It is the application's responsibility to create the folder when it doesn't exist. This may require some changes to your code to use the overload with a SpecialFolderOption.

    For example, the following code ensures the LocalApplicationData folder will be created if it doesn’t exist.

    // Use DoNotVerify in case LocalApplicationData doesn’t exist.
    string appData = Path.Combine(Environment.GetFolderPath(SpecialFolder.LocalApplicationData, SpecialFolderOption.DoNotVerify), "myapp");
    // Ensure the directory and all its parents exist.
    Directory.CreateDirectory(appData);
    

    Path.GetTempPath and Temp.GetTempFileName

    The System.IO.Path class has a method that returns the path of the current user's temporary folder:

    public static string GetTempPath ();
    

    Windows applications may assume the path returned here is user-specific. This is because the implementation picks up the USERPROFILE environment variable. When the variable is unset, the API returns the Windows temp folder.

    On Linux, the implementation returns /tmp. This folder is shared with other users. As a consequence, applications should use unique names to avoid conflicts with other applications. Furthermore, because the location is shared, other users will be able to read the files created here, so you should not store sensitive data in this folder. The first user to create a file or directory will own it. This can cause your application to fail when trying to create a file or directory that is already owned by another user.

    The Temp.GetTempFileName method solves these issues for creating files. It creates a unique file under GetTempPath that is only readable and writable by the current user.

    On Windows, the value returned by GetTempPath can be controlled using the TMP/TEMP environment variables. On Linux, this can be done using TMPDIR.

    On systems with systemd, like Fedora and Red Hat Enterprise Linux (RHEL), a user-private temporary directory is available and can be located using the XDG_RUNTIME_DIR environment variable.

    Conclusion

    In this article, you’ve seen the features and limitations of using Environment.GetFolderPath, Temp.GetTempPath and Temp.GetTempFileName in your cross-platform .NET Core applications.

    Here are some additional .NET Core articles that might be helpful:

    • Improving .NET Core Kestrel performance using a Linux-specific transport
    • Using OpenShift to deploy .NET Core applications
    • Running Microsoft SQL Server on Red Hat OpenShift
    • Securing .NET Core on OpenShift using HTTPS
    • Announcing .NET Core 2.1 for Red Hat Platforms

     

    Last updated: September 3, 2019

    Recent Posts

    • Red Hat Enterprise Linux 10.2 and 9.8: Top features for developers

    • What GPU kernels mean for your distributed inference

    • Debugging image mode with Red Hat OpenShift 4.20: A practical guide

    • EvalHub: Because "looks good to me" isn't a benchmark

    • SQL Server HA on RHEL: Meet Pacemaker HA Agent v2 (tech preview)

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.