David
David

Reputation: 656

Share File Between Applications

I am developing a reasonably complex process control background application on a Pi 3 (using c#, VC 2015). This is being developed and tested in a modular manner (display, user input, gpio extender boards, various types of sensors, relays, network comms, etc). Each module is built as a separate DLL and tested with its own background test app.

My problem is that I need to maintain a common set of data across all modules, particularly a set of application parameters. Also local storage as a cache for results and logging. So several different applications need to access this data during development - but only one at a time. Obviously in the final project, there will be a single application, so no problem.

I have been amazed to find that Win IoT does not seem to allow a simple file to be accessible to different applications. App Services and other inter-app communications all seem to be at the transaction level and not appropriate here. To build an app services facility to handle all I/o would be tedious(not ultimately required).

Does anyone have an idea as to how this situation could be managed sensibly, please?

Upvotes: 0

Views: 238

Answers (1)

Rita Han
Rita Han

Reputation: 9710

Due to File access permissions of UWP app. Reading data from a file in local storage seems unreachable.

So, if you are willing to use a removable device(external storage) attached to the Raspberry Pi and store your application parameters in a file, like a text file, and read the data from your apps.

I test the following code(UWP app) on desktop and read data successfully from multi apps. I am sure that you can read data on Raspberry Pi from one app but I didn't test multi apps. You can have a try. If there is any concern please feel free let me know.

        Task.Run( async () =>
        {
            var removableDevices = KnownFolders.RemovableDevices;

            var externalDrives = await removableDevices.GetFoldersAsync();

            var drive0 = externalDrives[0];

            var testFolder = await drive0.GetFolderAsync("test");

            var SharedDateFile = await testFolder.GetFileAsync("data.txt");

            var data = await FileIO.ReadTextAsync(SharedDateFile);

            System.Diagnostics.Debug.WriteLine(data);
        });

In the app manifest, you need specify the Removable Storage capability and register at least one File Type Association declaration.

UPDATE:

Using publisher folder is the better solution. First, add the following extension in Package.appxmanifest:

  <Extensions>
  <Extension Category="windows.publisherCacheFolders">
    <PublisherCacheFolders>
      <Folder Name="Folder1"/>
    </PublisherCacheFolders>
  </Extension>
  </Extensions>

Then write and read file like this:

        //Write file
        var folder = Windows.Storage.ApplicationData.Current.GetPublisherCacheFolder("Folder1");
        var file = await folder.CreateFileAsync("settings.txt", Windows.Storage.CreationCollisionOption.OpenIfExists);
        await FileIO.WriteTextAsync(file,"Hello writen by app1");

        //Read file
        var folder = Windows.Storage.ApplicationData.Current.GetPublisherCacheFolder("Folder1");
        var file = await folder.GetFileAsync("settings.txt");
        var text = await FileIO.ReadTextAsync(file);
        System.Diagnostics.Debug.WriteLine(text);

Upvotes: 2

Related Questions