标签:
主要内容:
使用选取器打开和保存文件
关于文件、文件夹和库,如果深究其实还是有比较多的内容,我们这一次来学习一下选取器就收了。还有上篇博文中读写文本文件的三种方式可以细细体会一下。
文件选取器包含文件打开选取器(FileOpenPicker、FolderPicker)和文件保存选取器(FileSavePicker),分别用来打开文件和保存文件,这两个选取器的使用方法大致相同。
FileOpenPicker类:
ViewMode ,获取或设置文件选取器用来展示文件或文件夹的视图模式,属性值由PickerViewMode枚举来指定,该枚举有两个枚举值,List表示列表模式,Thumbnail表示缩略图模式
SuggestedStartLocation ,获取或设置文件选取器要呈现给用户的文件的初始位置。属性值由PickerLocationId枚举指定,此枚举一共有10个枚举值,自行转到定义进行查看。
注意我们设置的ViewMode和SuggestedStartLocation值在应用程序第一次运行之后,再切换其他值运行,有时候并不会起作用,应该是操作系统自己有记录,然后这个FileTypeFilter属性是必须指定的。
FileOpenPicker picker = new FileOpenPicker(); //创建文件打开选取器
picker.ViewMode = PickerViewMode.Thumbnail; //将ViewMode的值设置为Thumbnail
picker.SuggestedStartLocation =PickerLocationId.PicturesLibrary; //将文件选取器打开文件的开始位置设置为图片库
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
FolderPicker类:
该类的属性和用法与FileOpenPicker类似。
FolderPicker folderPicker = new FolderPicker();
folderPicker.ViewMode = PickerViewMode.List;
folderPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
folderPicker.FileTypeFilter.Add("*");
FileSavePicker类:
该类我们经常使用到的3个属性是SuggestedStartLocation(同上)、FileTypeChoices、SuggestedFileName。FileTypeChoices是一个字典类型(IDictionary<System.String, IList<System.String>>),获取用户可选择分配到文件的有效文件类型的集合,SuggestedFileName 则是获取或设置文件保存选取器向用户建议的文件名。
FileSavePicker picker = new FileSavePicker();
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
picker.FileTypeChoices.Add("Image", new List<string>() { ".jpg", ".jpeg", ".png", ".bmp", ".gif" });
picker.SuggestedFileName =DateTime.Now.ToString("yyyyMMddhhmmss");
我们来进行常规演示,首先是选取单个文件,多个文件,还有文件夹。
选取并显示一张图片:
private async void btn_pickpic_Click(object sender, RoutedEventArgs e) { FileOpenPicker picker = new FileOpenPicker(); picker.ViewMode = PickerViewMode.List; picker.SuggestedStartLocation =PickerLocationId.PicturesLibrary; picker.FileTypeFilter.Add(".jpg"); picker.FileTypeFilter.Add(".jpeg"); picker.FileTypeFilter.Add(".png"); storagefile = await picker.PickSingleFileAsync(); if (storagefile != null) { this.tb_pickedpic.Text = "Picked photo: " + storagefile.Name; WriteableBitmap writeableBitmap = new WriteableBitmap(500, 500); IRandomAccessStream stream = await storagefile?.OpenAsync(FileAccessMode.Read); await writeableBitmap.SetSourceAsync(stream); image.Source = writeableBitmap; } else { this.tb_pickedpic.Text = "Operation cancelled."; image.Source = null; } }
选取多张图片:
private async void btn_pickmultipic_Click(object sender, RoutedEventArgs e) { var picker = new FileOpenPicker(); picker.ViewMode = PickerViewMode.Thumbnail; picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; picker.FileTypeFilter.Add(".jpeg"); picker.FileTypeFilter.Add(".png"); picker.FileTypeFilter.Add(".jpg"); IReadOnlyList<StorageFile> filelist =await picker.PickMultipleFilesAsync(); StringBuilder output = new StringBuilder("Picked files:"); if (filelist.Count>0) { foreach (var file in filelist) { output.Append(file.Name+"\n"); } } else { output.Append("none!"); } Showmsg(output.ToString()); }
Showmsg(output.ToString());是弹出对话框,显示所选择的图片。
选取文件夹:
private async void btn_pickfolder_Click(object sender, RoutedEventArgs e) { FolderPicker folderPicker = new FolderPicker(); folderPicker.ViewMode = PickerViewMode.List; folderPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; folderPicker.FileTypeFilter.Add("*"); StorageFolder folder = await folderPicker.PickSingleFolderAsync(); if (folder != null) { StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder); Showmsg("Picked folder: " + folder.Name); } else { Showmsg("Operation cancelled."); } }
接着是保存文件,首先是保存文本文件,然后将上面选取并显示的那张图片保存到另一文件夹。
保存文本文件:
private async void button_Click(object sender, RoutedEventArgs e) { FileSavePicker savePicker = new FileSavePicker(); savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; savePicker.FileTypeChoices.Add("文本文件", new List<string>() { ".txt" }); savePicker.SuggestedFileName = "mytxt"; StorageFile file = await savePicker.PickSaveFileAsync(); if (file != null) { CachedFileManager.DeferUpdates(file); if (tbxcontent.Text.Trim().Length > 0) { await FileIO.WriteTextAsync(file,tbxcontent.Text); FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file); if (status == FileUpdateStatus.Complete) { this.tbInfo.Text = "File " + file.Name + " was saved."; } else { this.tbInfo.Text = "File " + file.Name + " couldn‘t be saved."; } } } else { this.tbInfo.Text = "Operation cancelled."; } }
运行结果:
保存上面选取并显示的那张图片到另一文件夹:
private async void btn_save_Click(object sender, RoutedEventArgs e) { FileSavePicker picker = new FileSavePicker(); picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; picker.FileTypeChoices.Add("Image", new List<string>() { ".jpg", ".jpeg", ".png", ".bmp", ".gif" }); picker.SuggestedFileName =DateTime.Now.ToString("yyyyMMddhhmmss"); StorageFile file= await picker.PickSaveFileAsync(); if(file!=null&&storagefile!=null) { CachedFileManager.DeferUpdates(file); IBuffer buffer = await FileIO.ReadBufferAsync(storagefile); //storageFile是已经选中要保存的文件 await FileIO.WriteBufferAsync(file, buffer); FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file); } }
运行结果:
好了,这些就是关于文件选取器的一些内容了。下次见!
标签:
原文地址:http://www.cnblogs.com/czhwust/p/Win10_file2.html