Answered by:
[UWP] Create a FileStream in UWP

Question
-
I need to read a file from disk and feed a System.IO.Stream to the library (sharpkml) that will process it. I have an issue when executing the code in UWP, in WIN32 is fine.
This code works fine:Stream ioStream = System.IO.File.Open(relPath, FileMode.Open);
bool check = stream_NET is FileStream // returns always true
file = KmlFile.Load(ioStream);
When deploying to UWP I use the following code but the library throws an exception:
StorageFolder storageFolder = ApplicationData.Current.LocalFolder; StorageFile storageFile = await storageFolder.GetFileAsync(filename); var stream_WINRT = await storageFile.OpenReadAsync();
Stream stream_NET = stream_WINRT.AsStream(); // returns a IO.BufferedStream bool check = stream_NET is FileStream // returns always false
file = KmlFile.Load(stream_NET); // the library throws an exception
So the question is how do I get a FileStream on UWP? Because from my understanding that’s the issue.
- Edited by Bocci753 Monday, July 30, 2018 9:29 AM
Monday, July 30, 2018 9:28 AM
Answers
-
Hi,
APIs in System.IO.File namespace is available in UWP apps. You could refer this link:System.IO namespaces for UWP apps.
So you could still use the System.IO.File.Open() method like this:
StorageFolder storageFolder = ApplicationData.Current.LocalFolder; StorageFile storageFile = await storageFolder.GetFileAsync(filename); var stream = System.IO.File.Open(storageFile.Path,FileMode.Open); bool check = stream is FileStream;
Best regards,
Roy
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Proposed as answer by Roy LiMicrosoft contingent staff Wednesday, August 1, 2018 6:36 AM
- Marked as answer by Bocci753 Wednesday, August 1, 2018 7:24 AM
Tuesday, July 31, 2018 2:59 AM
All replies
-
Hi,
APIs in System.IO.File namespace is available in UWP apps. You could refer this link:System.IO namespaces for UWP apps.
So you could still use the System.IO.File.Open() method like this:
StorageFolder storageFolder = ApplicationData.Current.LocalFolder; StorageFile storageFile = await storageFolder.GetFileAsync(filename); var stream = System.IO.File.Open(storageFile.Path,FileMode.Open); bool check = stream is FileStream;
Best regards,
Roy
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Proposed as answer by Roy LiMicrosoft contingent staff Wednesday, August 1, 2018 6:36 AM
- Marked as answer by Bocci753 Wednesday, August 1, 2018 7:24 AM
Tuesday, July 31, 2018 2:59 AM -
Thanks Roy that obviously works fine!
I thought IO.File wasn't available under UWP and we had to use Windows.Storage methods. It's honestly a bit confusing but my bad for not checking.
Wednesday, August 1, 2018 7:24 AM