C# 从资源文件/内容获取流

C# 从资源文件/内容获取流,c#,asynchronous,microsoft-metro,windows-runtime,windows-store-apps,C#,Asynchronous,Microsoft Metro,Windows Runtime,Windows Store Apps,这是从资源文件获取流的正确/唯一方法吗 Uri uri = new Uri(fullPath); StorageFile storageFile = await Windows.Storage.StorageFile. GetFileFromApplicationUriAsync(uri); IRandomAccessStreamWithContentType randomAccessStream = await stora

这是从资源文件获取流的正确/唯一方法吗

    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和本地存储)都返回一个流对象,如果else代码使用一个或另一个流对象,那就很痛苦了

我也试着从中创建一个MemoryStream,但我甚至找不到一种方法来提取字节。。。请帮忙

    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
从资源读取时的IIInputStream.ReadAsync():

而Stream.Read()和Stream.ReadAsync()如下所示:

谢谢

好的,我找到了

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

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

您也可以在一行中完成:

Stream stream = await storageFile.OpenStreamForReadAsync(); 

是否可以将流转换为StorageFile?:oThank you very…you make my day:)之前尝试执行randomAccessStream-->Byte[]-->memorystream,但它无法正常工作。在行下DataReader.LoadAsync((uint)s.Size)上始终失败irandomaccesstream-->字节[];现在好好工作吧