Edward_jie

for you, my Hall of Frame

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  92 随笔 :: 45 文章 :: 539 评论 :: 43万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

 二进制文件一般被认为是一组序列字节。一般来说一个二进制文件可能包含任何形式的二进制编码的数据类型。例如:.mp3文件,.jpg文件,.db文件都可以看做二进制文件。本篇内容将以MP3文件为例。

  首先创建一个Windows Phone 7项目,在项目中添加一个MP3文件例如“Battery_Low.mp3”,然后在MainPage.xaml.cs(或其他页面文件)中引入命名空间:

using System.IO.IsolatedStorage;  

using System.IO;  

using System.Windows.Resources; 

 

  保存MP3文件到隔离存储空间

  示例中首先检查文件是否已经存在,然后把“Battery_Low.mp3”文件保存到隔离存储空间。

  我们首先创建一个文件流,然后使用BinaryWriter和BinaryReader在隔离层存储空间中创建一个新的MP3文件并且把“Battery_Low.mp3”的数据复制过去。

  提示:分块读取文件有利于减少内存消耗和提高性能。

01 private const string FileName = "Battery_Low.mp3";  

02 private void btnSave_Click(object sender, RoutedEventArgs e)  

03 {  

04     StreamResourceInfo streamResourceInfo = Application.GetResourceStream(new Uri(FileName, UriKind.Relative));  

05     

06     using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())  

07     {  

08         if (myIsolatedStorage.FileExists(FileName))  

09         {  

10             myIsolatedStorage.DeleteFile(FileName);  

11         }  

12     

13         using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(FileName, FileMode.Create, myIsolatedStorage))  

14         {  

15             using (BinaryWriter writer = new BinaryWriter(fileStream))  

16             {  

17                 Stream resourceStream = streamResourceInfo.Stream;  

18                 long length = resourceStream.Length;  

19                 byte[] buffer = new byte[32];  

20                 int readCount = 0;  

21                 using (BinaryReader reader = new BinaryReader(streamResourceInfo.Stream))  

22                 {  

23                     // read file in chunks in order to reduce memory consumption and increase performance  

24                     while (readCount < length)  

25                     {  

26                         int actual = reader.Read(buffer, 0, buffer.Length);  

27                         readCount += actual;  

28                         writer.Write(buffer, 0, actual);  

29                     }  

30                 }  

31             }  

32         }  

33     }  

34 } 

 

  从隔离存储空间中读取MP3文件

  示例中首先从隔离存储空间打开了一个名为Battery_Low.mp3的文件,并且把内容设置为一个媒体元素的数据源。

01 private void btnRead_Click(object sender, RoutedEventArgs e)  

02 {  

03     using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())  

04     {  

05         using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(FileName, FileMode.Open, FileAccess.Read))  

06         {  

07             this.mediaElement.SetSource(fileStream);  

08         }  

09     }  

10 } 

  提示:"mediaElement"是一个在MainPage.xaml中的媒体元素:<MediaElement x:Name="mediaElement" AutoPlay="True"/>

本文来自Zdave的博客,原文地址:http://www.cnblogs.com/zdave/archive/2011/06/09/2076337.html

posted on   Edward_诺  阅读(494)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示