本帖最后由 agameboy 于 2012-5-17 17:09 编辑 二进制文件 一般被认为是一组序列字节,这一章主要是读取、保存二进制文件 ,更多的文章请参考WP7 IsolatedStorage系列篇 ! 引用命名空间: using System.IO; using System.IO.IsolatedStorage; using System.Windows.Resources; 二进制文件 一般被认为是一组序列字节。一般来说一个二进制文件可能包含任何 形式的二进制编码的数据 类型。例如:.mp3文件,.jpg文件,.db文件都可以 看做二进制文件。本篇内容 将以MP3文件为例。 示例中首先检查文件是否已经存在,然后把“Hello.mp3 ”文件保存到隔离存储空间。我们 首先创建一个文件流,然后使用BinaryWriter和BinaryReader 在隔离层存储空间中创建一个新的MP3文件并且把“Hello.mp3 ”的数据复制过去。 提示:分块读取文件有利于减少内存消耗和提高性能。 定义变量名称: const string strFileName = "Hello.mp3"; 保存MP3:
private void button1_Click(object sender, RoutedEventArgs e) { StreamResourceInfo streamresouceinfo = Application.GetResourceStream(new Uri(strFileName, UriKind.Relative)); using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) { if (myIsolatedStorage.FileExists(strFileName)) { myIsolatedStorage.DeleteFile(strFileName); } using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(strFileName, FileMode.Create, myIsolatedStorage)) { using (BinaryWriter binarywriter = new BinaryWriter(fileStream)) { Stream resoucestream = streamresouceinfo.Stream; long llength = resoucestream.Length; byte[] buffer = new byte[32]; int readCount = 0; using (BinaryReader binaryreader = new BinaryReader(streamresouceinfo.Stream)) { while (readCount < llength) { int dActual = binaryreader.Read(buffer, 0, buffer.Length);//先读取到数组中 readCount += dActual; binarywriter.Write(buffer, 0, dActual);//保存 } } } } } }
复制代码
读取MP3:
private void button2_Click(object sender, RoutedEventArgs e) { using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(strFileName, FileMode.Open, FileAccess.Read)) { this.mediaElement1.SetSource(fileStream); } } }
复制代码
转载于:https://www.cnblogs.com/Belling/archive/2012/11/29/2794600.html