Michael S. Scherotter
Michael S. Scherotter

Reputation: 10785

ApplicationData.Current.TemporaryFolder.CreateFileAsync() fails from desktop app

I am creating a WPF desktop app that accesses the Windows 10 Runtime APIs.

If I use the ApplicationData.Current.TemporaryFolder.CreateFileAsync() API it fails with an InvalidOperationException

HResult=-2147009196
Message=The process has no package identity. (Exception from HRESULT: 0x80073D54)

Is it possible to use this API from a WPF desktop App? Can I add a package identity to it? How else can I create a StorageFile in a Windows desktop app?

Upvotes: 2

Views: 1405

Answers (2)

kennyzx
kennyzx

Reputation: 13003

A desktop application does not have a package identity. You can use Desktop App Converter (DAC) to generate a package for your desktop app. After packaging the WPF exe as an appx using DAC, and installing the appx, the app works as expected.

DAC requires the Pro or Enterprise edition, though.

But you can use StorageFolder/StorageFile directly from desktop app without having to convert the app to package before hand. The error you encountered was because ApplicationData.Current maps to a folder which does not exist for a desktop app.

This piece of code creates and writes to a file in temporary folder using WinRT APIs.

var folder = await StorageFolder.GetFolderFromPathAsync(System.IO.Path.GetTempPath());
var file = await folder.CreateFileAsync("helloworld.txt");            
await FileIO.WriteTextAsync(file, "hahahaha");

Upvotes: 2

Glen Thomas
Glen Thomas

Reputation: 10764

The StorageFile type is only supported for Windows Store apps. It is used because the apps are sandboxed and memory optimized. In WPF you have full access to the hard disk, so could just write data out to any file, any place you want, or just store it in process memory if it's not too big.

I think the equivalent in WPF would be to use Path.GetTempFileName to get a path to a temporary file and then use some file writer class to write to disk.

Upvotes: 0

Related Questions