fandro
fandro

Reputation: 4903

Get data from another UWP application

I wan't to access data stored in another UWP application. I saw that you can save data in the LocalState folder of your UWP application using this code :

Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter =
            new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime");

StorageFile sampleFile = await localFolder.CreateFileAsync("example.txt",
                         CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(sampleFile, formatter.Format(DateTime.Now));

Here is the official documentation.

So with this code I create a file called example.txt in my first UWP project.

Is it possible to get access to the example.txt from a second UWP project ? If yes, how ?

Upvotes: 0

Views: 833

Answers (3)

saurabh
saurabh

Reputation: 724

You can also create an AppService which can share any data. Basically the provider will host the service and the app who needs data should consume the service.

More info : https://learn.microsoft.com/en-us/windows/uwp/launch-resume/how-to-create-and-consume-an-app-service

Upvotes: 1

AVK
AVK

Reputation: 3923

Yes. This is possible only if both apps are published by same publisher.

This folder cache is called PublisherCacheFolder

Here is a Blog Post that will explain this in detail.

You need to Add an extension in your appmanifest before this can be done in all the apps that you prefer to share data.

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

There is a good video explaining about this on Channel9 . Jump to 19th Minute.

Below are simple Write and Read Methods from another Blog.

async void WritetoPublisherFolder(string Text)  
{  
    StorageFolder SharedFolder = Windows.Storage.ApplicationData.Current.GetPublisherCacheFolder("CommonFolder");  
    StorageFile newFile = await SharedFolder.CreateFileAsync("SharedFile.txt", CreationCollisionOption.OpenIfExists);  
    await FileIO.WriteTextAsync(newFile, Text);  
}  

async Task<string> ReadFromSharedFolder()  
{  
    StorageFolder SharedFolder = Windows.Storage.ApplicationData.Current.GetPublisherCacheFolder("CommonFolder");  
    StorageFile newFile = await SharedFolder.GetFileAsync("SharedFile.txt");  
    var text = await FileIO.ReadTextAsync(newFile);  
    return text;  
}

Upvotes: 1

Romasz
Romasz

Reputation: 29792

Applications in UWP are sandboxed, thus you cannot so easily access their storage. There are couple of options for sharing data:

Upvotes: 2

Related Questions