WP7&WP8开发、文件读写操作上的分别

【菜科解读】
一、WP7的文件操作:如果在wp7平台上去写入一个文件,我们会使用: IsolatedStorageFile代码如下:①写入文件private void WriteFile(string fileName, string content){ using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream isolatedStorageFileStream = isolatedStorageFile.CreateFile(fileName)) { using (StreamWriter streamWriter = new StreamWriter(isolatedStorageFileStream)) { streamWriter.Write(content); } } }}②读取文件private string ReadFile(string fileName){ string text; using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream isolatedStorageFileStream = isolatedStorageFile.OpenFile(fileName, FileMode.Open)) { using (StreamReader streamReader = new StreamReader(isolatedStorageFileStream)) { text = streamReader.ReadToEnd(); } } } return text;}二、WP8文件操作wp8与win8的文件操作方式类似,如果你在win8下写过文件操作,那么WP8自然熟悉,这需要用到2个接口:IStorageFile和 IStorageFolder, 可以看出,一个是对文件的操作,一个是对目录的。
这两个接口均在 :Windwos.Storage组件中,注意:因在windows 8 开发中大量使用了WinRT异步编程方式,故在WP8中编程亦如此。
代码如下:①写入文件:public async Task WriteFile(string fileName, string text){ IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder; IStorageFile storageFile = await applicationFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); using (Stream stream = await storageFile.OpenStreamForWriteAsync()) { byte[] content = Encoding.UTF8.GetBytes(text); await stream.WriteAsync(content, 0, content.Length); }}②读取文件:public async Task ReadFile(string fileName){ string text; IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder; IStorageFile storageFile = await applicationFolder.GetFileAsync(fileName); IRandomAccessStream accessStream = await storageFile.OpenReadAsync(); using (Stream stream = accessStream.AsStreamForRead((int)accessStream.Size)) { byte[] content = new byte[stream.Length]; await stream.ReadAsync(content, 0, (int) stream.Length); text = Encoding.UTF8.GetString(content, 0, content.Length); } return text;}三、兼容性问题上面的代码分别对WP7与WP8平台的文件写入与读取操作进行了简单的说明那么在WP7应用中的文件目录结构定义,是不是在WP8中也是一样的呢?其实,两者是有点区别的,这点区别,也是日后WP7向WP8迁移过程中必须要考虑的元素之一。
在WP7中,创建文件test.txt,那么,它在隔离存储区的文件目录结构如下:C:\Data\Users\DefaultAppAccount\AppData\{ProductID}\Local\IsolatedStore\test.txt在WP8中,创建相同文件,文件存储目录结构如下:C:\Data\Users\DefaultAppAccount\AppData\{ProductID}\Local\test.txt两者一对比,我们发现:在WP8开发中,如果使用现有的WP7代码时,如果你不想改变原有文件目录结构,那你就必须要首先获取WP7隔离存储目录.即:IStorageFolder applicationFolder =await ApplicationData.Current.LocalFolder.GetFolderAsync("IsolatedStore");最后,想说明的是:如果你想“偷懒”或者说减少开发工作量,就看看我上面说的这些但如果打算做WP8/WIN8两个平台,最好全新开发基于WP8平台的应用,这可以加快你的Win8应用的移植。
WP8开发从SD卡读取音频文并设置为手机铃声
XAML步骤 2: 注册扩展若要处理特定的文件类型,注册文件关联扩展名。
在 WMAppManifest.xml 中指定的扩展名。
后的标记元素,里面的扩展元素,只是下面的FileTypeAssociation元素与指 定文件关联扩展名。
1 4 Assets/Route_Mapper_Logo33x33.png 5 Assets/Route_Mapper_Logo69x69.png 6 Assets/Route_Mapper_Logo176x176.png 9 .yumi12 步骤3: 从 SD 卡中获取文件// 连接当前的SDCardExternalStorageDevice _sdCard = (await ExternalStorage.GetExternalStorageDevicesAsync()).FirstOrDefault();// 如果sd卡存在连接Ringstone文件夹.if (_sdCard != null){ try { ExternalStorageFolder routesFolder = await _sdCard.GetFolderAsync("Ringstones"); IEnumerable routeFiles = await routesFolder.GetFilesAsync(); foreach (ExternalStorageFile esf in routeFiles) { if (esf.Path.EndsWith(".yumi")) { Routes.Add(esf); } } } catch (FileNotFoundException) { MessageBox.Show("你的SD卡上的Ringstones文件夹丢失。
添加一个Ringstones文件夹中包含至少一个yumi文件,然后再试一次."); }}else { MessageBox.Show("你的SD卡上的Ringstones文件夹丢失。
添加一个Ringstones文件夹中包含至少一个yumi文件,然后再试一次.");}4 对读到的文件流进行转换成mp3格式并设为手机铃声 private async void SetRingClick( ExternalStorageFile ef) Stream s = await ef.OpenForReadAsync(); SaveRing(s, ef.Name);//检查系统中是否存在YumiRings文件夹 private void CreateDir() if (store.DirectoryExists("YumiRings") == false) store.CreateDirectory("YumiRings");//保存为铃声 private void SaveRing(Stream s,string name)//将读取到的.yumi格式的文件进行转储到软件中并重新更改文件格式 string filename = name.Replace("yumi", "mp3"); CreateDir(); if (store.FileExists("YumiRings" + filename)) store.DeleteFile("YumiRings" + filename); IsolatedStorageFileStream newstream = new IsolatedStorageFileStream("YumiRings//" + filename, FileMode.Create, store); BinaryWriter newwriter = new BinaryWriter(newstream); BinaryReader tempreader = new BinaryReader(s); int filesize = (int)s.Length; byte[] TempBytes = new byte[filesize]; TempBytes = tempreader.ReadBytes(filesize); newwriter.Write(TempBytes); newwriter.Close(); newstream.Close(); SetRing(filename); catch (Exception) return; ///启动手机中保存铃声的Task实现铃声的保存 private void SetRing(string name) SaveRingtoneTask saveRingtoneChooser = new SaveRingtoneTask(); saveRingtoneChooser.Completed += new EventHandler(saveRingtoneChooser_Completed); saveRingtoneChooser.Source = new Uri("isostore:/YumiRings/" + name); saveRingtoneChooser.DisplayName = name.Substring(0, name.IndexOf(".")); saveRingtoneChooser.Show(); catch (System.InvalidOperationException e) MessageBox.Show(e.Message); public void saveRingtoneChooser_Completed(object sender, TaskEventArgs e) switch (e.TaskResult) case TaskResult.OK: MessageBox.Show("OK"); break; case TaskResult.Cancel: MessageBox.Show("No"); break; case TaskResult.None: MessageBox.Show("No"); break; }源码获取请加QQ群:157153754。
WP8,开发,从,卡,读取,音频,文,并,设置,为,首,
WindowsPhone8开发工具包、wp8开发资源汇总
我想把目前找到的关于Windows Phone 8最新的开发资源汇总一下分享给大家。
Windows Phone SDK 8.0下载地址:http://www.microsoft.com/zh-cn/download/details.aspx?id=35471;注意:推荐大家下载ISO文件,大家可以将下面这个链接直接拷贝到下载工具直接下载,ISO文件大约1.6G;http://download.microsoft.com/download/F/5/6/F56AD199-EF12-43C7-8551-C095394D3B32/fulltril30/iso/wpsdkv80_chs1.iso操作系统要求:1.Windows 8 64位(x64);PS:因此想开发Windows Phone 8的只能在本机或虚拟机中安装Windows 8了。
硬件要求:1.最少4G的可用硬盘空间;2.最少4G RAM;3.64位 CPU;PS:内存要求4G,有点狠啊!对于大多数的2G笔记本,最好加内存了。
。
。
。
。
Windows Phone 8 模拟器:1.Windows 8 Pro版本或更高版本;2.要求支持二级地址转换(SLAT)的处理器;如果我们的电脑硬件和系统都符合要求了,但是不符合模拟器的要求,也会安装和运行Windows Phone SDK 8.0 ,此时Windows Phone 8模拟器就不起任何作用了,也就是说我们就没有办法在Windows Phone 8模拟器上部署和测试应用。
Windows Phone 官方博客http://blogs.windows.com/windows_phone/b/windowsphone/如果我们对英文阅读吃力的话,可以在网页的右边找到“Translate”选择简体中文,翻译成中文就行了。
全新的Windows Phone 开发中心地址:http://dev.windowsphone.com/zh-cn;我们可以从“作品展示”提供的视频进行学习了解Windows Phone 8。
Visual Studio博客中关于Windows Phone SDK 8.0的介绍Introducing Windows Phone SDK 8.0地址:http://blogs.msdn.com/b/visualstudio/archive/2012/10/30/introducing-windows-phone-sdk-8-0.aspx目前我能够找到的关于Windows Phone 8开发的相关资源都分享给大家了,如果大家还有更好的资源希望留言分享出来! WindowsPhone8,开发,工具包,、,wp8,开发资