WPF C# package Deployment: Best place to store text file

CyberSteve 156 Reputation points
2023-05-12T16:05:18.2833333+00:00

I have a simple Windows 10 WPF packaged app which will be deployed using MSIX. Everything is all configured with an application packaging project.

 

However, I have one single text file which contains items for a listview which is created and read / written to at run-time.

 

Where would be the best location to store this file? I’m thinking to use the local data storage folder. I know this works well for UWP with isolated storage, but how would I do this in WPF?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,709 questions
0 comments No comments
{count} votes

Accepted answer
  1. don bradman 621 Reputation points
    2023-05-13T06:15:43.25+00:00

    When it comes to storing files in a Windows 10 WPF packaged app, there are several options available. The best location to store a text file that contains items for a listview would be the local data storage folder. This folder is specifically designed for storing application data that needs to be accessed by the app at run-time.

    To access the local data storage folder in a WPF app, you can use the following code:

    string localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
    string appDataFolder = Path.Combine(localAppData, "YourAppName");
    
    if (!Directory.Exists(appDataFolder))
    {
        Directory.CreateDirectory(appDataFolder);
    }
    
    string filePath = Path.Combine(appDataFolder, "YourTextFile.txt");
    

    This code retrieves the path to the local data storage folder and creates a subfolder for your app if it doesn't already exist. It then creates a file path for your text file within that subfolder. You can then use this file path to read and write to your text file at run-time.

    It's important to note that the local data storage folder is specific to the user and the app. This means that each user who installs your app will have their own local data storage folder, and the data stored in that folder will only be accessible by your app. This is similar to the isolated storage used in UWP apps.

    In summary, the best location to store a text file that contains items for a listview in a Windows 10 WPF packaged app is the local data storage folder. You can access this folder using the code provided above, which creates a subfolder for your app and a file path for your text file within that subfolder.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful