Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

Where is FileStream at the .NET for Windows Store?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.7k users

...