uwp對文件的操作和wpf,winform等等有很大的不同,主要原因是uwp對權限的要求比較嚴格,不能想從前那樣隨心所欲的讀取文件。
1.首先找到Package.appxmanifest這個文件,在功能里面勾選需要的功能,在申明里添加
,在此之后才能安心寫代碼。

2.打開文件選擇器,選擇文件夾,並保存選擇的文件夾。
//打開文件選擇器 FolderPicker pick = new FolderPicker(); pick.FileTypeFilter.Add(".png"); pick.FileTypeFilter.Add(".jpg"); pick.FileTypeFilter.Add(".bmp"); IAsyncOperation<StorageFolder> folderTask = pick.PickSingleFolderAsync(); StorageFolder folder = await folderTask; //var folder = await pick.PickSingleFolderAsync(); StorageFolder Folder = null; string Address; string Token = ""; if (folder != null) { Folder = folder; Address = folder.Path; Token = StorageApplicationPermissions.FutureAccessList.Add(folder); } StorageApplicationPermissions.FutureAccessList.GetFolderAsync(Token); //獲取本地文件夾 StorageFolder folderLocal = ApplicationData.Current.LocalFolder; //創建一個文件夾account try { folderLocal = await folderLocal.GetFolderAsync(folderStr); } catch (FileNotFoundException) { folderLocal = await folderLocal.CreateFolderAsync(folderStr); } StorageFile file = await folderLocal.CreateFileAsync( folderStr + ".json", CreationCollisionOption.ReplaceExisting); //保存選擇的文件夾Token var json = JsonSerializer.Create(); ImagePath imagePath = new ImagePath { Id = DateTime.Now.ToString("yyMMddHHmmss"), Path = Token }; string imageJson = imagePath.Stringify(); if (file != null) { try { using (StorageStreamTransaction transaction = await file.OpenTransactedWriteAsync()) { using (DataWriter dataWriter = new DataWriter(transaction.Stream)) { dataWriter.WriteInt32(Encoding.UTF8.GetByteCount(imageJson)); dataWriter.WriteString(imageJson); transaction.Stream.Size = await dataWriter.StoreAsync(); await transaction.CommitAsync(); } } } catch (Exception ex) { throw ex; } }
3.獲取已選擇文件夾下的圖片
StorageFile fileLocal = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///local/account/" + ImageHelper.folderStr + ".json")); if (fileLocal != null) { try { //讀取本地文件內容,並且反序列化 using (IRandomAccessStream readStream = await fileLocal.OpenAsync(FileAccessMode.Read)) { using (DataReader dataReader = new DataReader(readStream)) { UInt64 size = readStream.Size; if (size <= UInt32.MaxValue) { await dataReader.LoadAsync(sizeof(Int32)); Int32 stringSize = dataReader.ReadInt32(); await dataReader.LoadAsync((UInt32)stringSize); string fileContent = dataReader.ReadString((uint)stringSize); ImagePath imagePath = new ImagePath(fileContent); StorageFolder folder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync(imagePath.Path); //篩選圖片 var queryOptions = new Windows.Storage.Search.QueryOptions(); queryOptions.FileTypeFilter.Add(".png"); queryOptions.FileTypeFilter.Add(".jpg"); queryOptions.FileTypeFilter.Add(".bmp"); var query = folder.CreateFileQueryWithOptions(queryOptions); var files = await query.GetFilesAsync(); ImagePath img; imgList = new ObservableCollection<ImagePath>(); foreach (var item in files) { IRandomAccessStream irandom = await item.OpenAsync(FileAccessMode.Read); //對圖像源使用流源 BitmapImage bitmapImage = new BitmapImage(); bitmapImage.DecodePixelWidth = 160; bitmapImage.DecodePixelHeight = 100; await bitmapImage.SetSourceAsync(irandom); img = new ImagePath(); img.Path = item.Path; img.File = bitmapImage; img.Storage = item; imgList.Add(img); } imageView.ItemsSource = imgList; } } } } catch (Exception exce) { await new MessageDialog(exce.ToString()).ShowAsync(); throw exce; } }
最后的實現顯現效果大概如下:

/*----------------------------------------------更新----------------------------------------------*/
謝謝yinyue200 的提醒,Package.appxmanifest可以不用配置。
