获取一个流从一个资源文件/内容 [英] Getting a Stream from a resource file / content

查看:99
本文介绍了获取一个流从一个资源文件/内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是得到一个从资源文件的正确/唯一途径?

Is this the correct/only way of getting a Stream from a resource file?

    Uri uri = new Uri(fullPath);

    StorageFile storageFile = 
      await Windows.Storage.StorageFile.
        GetFileFromApplicationUriAsync(uri);

    IRandomAccessStreamWithContentType randomAccessStream = 
      await storageFile.OpenReadAsync();

    IInputStream resourceStream = (IInputStream)
      randomAccessStream.GetInputStreamAt(0);

我所有其它信号源(HTTP和本地存储)返回一个Stream对象,而且它是痛苦不得不使用一种或另一种的if-else code。

All my other sources (http and local storage) return a Stream object, and it is painful to have to if-else code that uses one or the other.

我也试着只创建一个MemoryStream出来,但我甚至找不到一个办法让字节出......请帮助。

I've also tried to just create a MemoryStream out of it, but I can't even find a way to get the bytes out... Please help.

    uint size = (uint)randomAccessStream.Size;
    IBuffer buffer = new Windows.Storage.Streams.Buffer(size);
    await randomAccessStream.ReadAsync(buffer, size, 
      InputStreamOptions.None);

    Stream stream = new MemoryStream(buffer); // error takes byte[] not IBuffer

IInputStream.ReadAsync()从资源读取时:
<一href=\"http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.streams.iinputstream.readasync.aspx\">http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.streams.iinputstream.readasync.aspx

而Stream.Read()和Stream.ReadAsync()是这样的:

while Stream.Read() and Stream.ReadAsync() look like this:

<一个href=\"http://msdn.microsoft.com/en-us/library/system.io.stream.read.aspx\">http://msdn.microsoft.com/en-us/library/system.io.stream.read.aspx

<一个href=\"http://msdn.microsoft.com/en-us/library/hh137813.aspx\">http://msdn.microsoft.com/en-us/library/hh137813.aspx

感谢

推荐答案

好,我找到了!

    StorageFile storageFile =
      await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);

    var randomAccessStream = await storageFile.OpenReadAsync();
    Stream stream = randomAccessStream.AsStreamForRead();

这篇关于获取一个流从一个资源文件/内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆