none
metro IBuffer怎么转换成IRandomAccessStream(C#) RRS feed

答案

  •         private async void Button_Click_1(object sender, RoutedEventArgs e)
            {
    
                IBuffer buffer;
    
                // convert string to IBuffer via InMemoryRandomAccessStream
                using (InMemoryRandomAccessStream memoryStream = new InMemoryRandomAccessStream())
                {
                    using (DataWriter dataWriter = new DataWriter(memoryStream))
                    {
                        dataWriter.WriteString("test string");
                        buffer = dataWriter.DetachBuffer();
                    }
                }
    
                // create the file in local folder "C:\Users\[use name]\AppData\Local\Packages\[app name]\LocalState\"
                // name is "sample.txt"
                StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("sample.txt", CreationCollisionOption.ReplaceExisting);
    
                // get the IRandomAccessStream from the file
                using (IRandomAccessStream writeStream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    using (DataWriter dataWriter = new DataWriter(writeStream))
                    {
                        // write the IBuffer to the IRandomAccessStream
                        dataWriter.WriteBuffer(buffer);
                        await dataWriter.StoreAsync();
                        await dataWriter.FlushAsync();
                    }
                }
            }

    我们可以通过DataWriter将IBuffer写入已知的IRandomAccessStream, 或者通过DataReader将IRandomAccessStream内容读入IBuffer

    更多关于文件操作,请参考例子: http://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    2012年3月28日 8:17
    版主