Where is FileStream at the .NET for Windows Store?

Craig Stevensson picture Craig Stevensson · Jan 13, 2013 · Viewed 9.5k times · Source

I'm creating a Windows Store app. I need to create a FileStream in order to write some complex data for a proprietary file format. I add System.IO to my uses, but there's no FileStream available.

I've investigating some more, and the ".NET for Windows Store apps overview" guide talks about IsolatedStorage, which this library don't even use currently. After some reading, I think the real replacement could be FileRandomAccessStream, from the nacemspace: Windows.Storage.Streams

What is the real equivalent to FileStream to use in a Windows Store app?

Answer

Erik picture Erik · Jan 13, 2013

Indeed, the sandbox has many limitations in where you can read from/write to. Here are some typical locations:

If you are trying to load resources from your application's installation folder, you can use the following:

  • StorageFolder installFolder = Windows.ApplicationModel.Package.Current.InstalledLocation

  • StorageFile file = await StorageFile.GetFileFromApplicationUriAsync("ms-appx:///file.txt");

(It is a read-only folder, so you cannot edit or create new files.)

If you are trying to write data files to your application's data folder, you can use the following:

  • StorageFolder localFolder = ApplicationData.Current.LocalFolder;

  • StorageFile file = await StorageFile.GetFileFromApplicationUriAsync("ms-appdata:///local/file.txt");

(This folder is read-write. You can also access roaming or temporary folders by changing local to one of the other two.)

There are some other folders as well, such as the DownloadsFolder, although you can only access files that your application downloads.

Alternatively, you can always ask the user for permission with FileOpenPicker and FileSavePicker. The pickers do not allow access to the InstalledLocation path, but will allow you to access Documents, Pictures, and Downloads (even if your app did not download the file).