本文整理汇总了C#中MemoryStream.AsRandomAccessStream方法的典型用法代码示例。如果您正苦于以下问题:C# MemoryStream.AsRandomAccessStream方法的具体用法?C# MemoryStream.AsRandomAccessStream怎么用?C# MemoryStream.AsRandomAccessStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MemoryStream
的用法示例。
在下文中一共展示了MemoryStream.AsRandomAccessStream方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Open
public async void Open(TextBox display)
{
try
{
FileOpenPicker picker = new FileOpenPicker();
picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
picker.FileTypeFilter.Add(textExt);
picker.FileTypeFilter.Add(compressedExt);
StorageFile file = await picker.PickSingleFileAsync();
switch (file.FileType)
{
case textExt:
display.Text = await FileIO.ReadTextAsync(file);
break;
case compressedExt:
using (MemoryStream stream = new MemoryStream())
using (IInputStream input = await file.OpenSequentialReadAsync())
using (Decompressor decompressor = new Decompressor(input))
using (IRandomAccessStream output = stream.AsRandomAccessStream())
{
long inputSize = input.AsStreamForRead().Length;
ulong outputSize = await RandomAccessStream.CopyAsync(decompressor, output);
output.Seek(0);
display.Text = await new StreamReader(output.AsStream()).ReadToEndAsync();
Show(string.Format("Decompressed {0} bytes to {1} bytes",
inputSize, outputSize), "Compression App");
}
break;
default:
break;
}
}
catch
{
}
}
示例2: Save
public async void Save(TextBox display)
{
try
{
StringBuilder text = new StringBuilder();
FileSavePicker picker = new FileSavePicker();
picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
picker.FileTypeChoices.Add("Text File", new List<string>() { textExt });
picker.FileTypeChoices.Add("Compressed File", new List<string>() { compressedExt });
picker.DefaultFileExtension = textExt;
StorageFile file = await picker.PickSaveFileAsync();
switch (file.FileType)
{
case textExt:
await FileIO.WriteTextAsync(file, display.Text);
break;
case compressedExt:
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(display.Text)))
using (IRandomAccessStream input = stream.AsRandomAccessStream())
using (IRandomAccessStream output = await file.OpenAsync(FileAccessMode.ReadWrite))
using (Compressor compressor = new Compressor(output.GetOutputStreamAt(0), algorithm, 0))
{
ulong inputSize = await RandomAccessStream.CopyAsync(input, compressor);
bool finished = await compressor.FinishAsync();
ulong outputSize = output.Size;
Show(string.Format("Compressed {0} bytes to {1} bytes",
inputSize, outputSize), "Compression App");
}
break;
default:
break;
}
}
catch
{
}
}