Search code examples
uwpwindows-store-apps

UWP app: How can I deploy updatable Json file?


How can I package a json file with my Win10 app so that it appears as an independent file in the LocalState (or sub) folder? I need for my app to be able to update this file occasionally.

If this is impossible can the app place files in some app data folder during the install?


Solution

  • You can mark that file as "Content" and "Copy Always" and on app start-up, you can copy that to LocalFolder programmatically and then update it whenever you want.

    For example: Let's say you have a "Config" folder under root project and test.json is the file you want to replace then

    string testPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "test.json");
    
    if (!File.Exists(testPath))
        {
                string tempPath = "ms-appx:///Config/test.json";
                Uri location = new Uri(tempPath, UriKind.Absolute);
                StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(location);
                await file.CopyAsync(ApplicationData.Current.LocalFolder);
        }