The UWP has a big difference in the way files are manipulated, wpf,winform, and so on, mainly because the UWP has a strict need for permissions, and cannot read files as freely as they once did.
1. First find package.appxmanifest This file, in the function check the required functions, add in the declaration, after this can be relieved to write code.
2. Open the file selector, select the folder, and save the selected folder.
//Open File SelectorFolderpicker pick =NewFolderpicker (); Pick. Filetypefilter.add (". PNG"); Pick. Filetypefilter.add (". jpg"); Pick. Filetypefilter.add (". bmp"); Iasyncoperation<StorageFolder> Foldertask =pick. Picksinglefolderasync (); Storagefolder folder=awaitFoldertask; //var folder = await pick. Picksinglefolderasync ();Storagefolder Folder =NULL; stringAddress; stringToken =""; if(Folder! =NULL) {Folder=folder; Address=folder. Path; Token=StorageApplicationPermissions.FutureAccessList.Add (folder); } StorageApplicationPermissions.FutureAccessList.GetFolderAsync (Token); //Get local FolderStoragefolder folderlocal =ApplicationData.Current.LocalFolder; //Create a folder account Try{folderlocal=awaitFolderlocal.getfolderasync (FOLDERSTR); } Catch(filenotfoundexception) {folderlocal=awaitFolderlocal.createfolderasync (FOLDERSTR); } StorageFile File=awaitFolderlocal.createfileasync (Folderstr+". JSON", creationcollisionoption.replaceexisting); //Save the selected folder token varJSON =jsonserializer.create (); ImagePath ImagePath=NewImagePath {Id = DateTime.Now.ToString ("Yymmddhhmmss"), Path =Token}; stringImagejson =imagepath.stringify (); if(File! =NULL) { Try { using(Storagestreamtransaction transaction =awaitfile. Opentransactedwriteasync ()) {using(DataWriter DataWriter =NewDataWriter (transaction. Stream) {Datawriter.writeint32 (Encoding.UTF8.GetByteCount (Imagejson)); Datawriter.writestring (Imagejson); Transaction. Stream.size=awaitDatawriter.storeasync (); awaitTransaction.commitasync (); } } } Catch(Exception ex) {Throwex; } }
3. Get the picture under the selected folder
StorageFile filelocal =awaitStoragefile.getfilefromapplicationuriasync (NewUri ("ms-appdata:///local/account/"+ Imagehelper.folderstr +". JSON")); if(Filelocal! =NULL) { Try { //reads local file contents, and deserializes using(Irandomaccessstream Readstream =awaitFilelocal.openasync (Fileaccessmode.read)) { using(DataReader DataReader =NewDataReader (Readstream)) {UInt64 size=readstream.size; if(Size <=uint32.maxvalue) {awaitDatareader.loadasync (sizeof(Int32)); Int32 stringsize=Datareader.readint32 (); awaitDatareader.loadasync ((UInt32) stringsize); stringFilecontent = Datareader.readstring ((UINT) stringsize); ImagePath ImagePath=NewImagePath (filecontent); Storagefolder folder=awaitStorageApplicationPermissions.FutureAccessList.GetFolderAsync (Imagepath.path); //Filter Pictures varQueryoptions =NewWindows.Storage.Search.QueryOptions (); QUERYOPTIONS.FILETYPEFILTER.ADD (". PNG"); QUERYOPTIONS.FILETYPEFILTER.ADD (". jpg"); QUERYOPTIONS.FILETYPEFILTER.ADD (". bmp"); varquery =folder. Createfilequerywithoptions (queryoptions); varFiles =awaitquery. Getfilesasync (); ImagePath img; Imglist=NewObservablecollection<imagepath>(); foreach(varIteminchfiles) {Irandomaccessstream IRandom=awaititem. OpenAsync (Fileaccessmode.read); //using a stream source for an image sourceBitmapImage BitmapImage =NewBitmapImage (); Bitmapimage.decodepixelwidth= the; Bitmapimage.decodepixelheight= -; awaitBitmapimage.setsourceasync (irandom); IMG=NewImagePath (); Img. Path=item. Path; Img. File=BitmapImage; Img. Storage=item; Imglist.add (IMG); } Imageview.itemssource=imglist; } } } } Catch(Exception exce) {await NewMessagedialog (exce. ToString ()). Showasync (); ThrowExce; } }
The results of the final implementation are probably as follows:
UWP picks folders and reads pictures from them