[Wanli journey-Windows App development] file & data-file selector, Wanli journey app development
Use the file selector to save the file
Personally, I still like to use the file selector because I can use my own code to call various system bullet boxes.
In this example, a Button and a TextBlock are added to XAML and named btnSaveFile and tBlockSaveInfo respectively. You can easily save the file in the Click Event of the background.
Private async void btnSaveFile_Click (object sender, RoutedEventArgs e) {FileSavePicker saveFile = new FileSavePicker (); saveFile. suggestedStartLocation = PickerLocationId. documentsLibrary; // The saveFile type displayed in the drop-down list. fileTypeChoices. add ("batch file", new List <string> (){". bat "}); // default file name saveFile. suggestedFileName = "SaveFile"; StorageFile file = await saveFile. pickSaveFileAsync (); if (file! = Null) {// stop updating the CachedFileManager file before the user completes the change and calls CompleteUpdatesAsync. deferUpdates (file); string fileContent = "@ echo off \ n dir/s \ n pause"; await FileIO. writeTextAsync (file, fileContent); // when the change is completed, other applications can change the file. FileUpdateStatus updateStatus = await CachedFileManager. CompleteUpdatesAsync (file); if (updateStatus = FileUpdateStatus. Complete) {tBlockSaveInfo. Text = file. Name + "has been saved. ";} Else {tBlockSaveInfo. Text = file. Name +" failed to save. ";}} Else {tBlockSaveInfo. Text =" The save operation is canceled. ";}}
The file type in the drop-down list in the Code is as follows.
I have added most of the content to the Code through comments. As for what the code of fileContent really means, you can try it and I feel quite interesting. 3 lines of code list all files and folders on the hard disk
If you have tried to open this bat file, Is it interesting?
What's more, the code we just wrote can be directly used on Windows Phone without modification. My Lumia 638 has been installed in the Windows 10 preview version. You can check out the new resource manager.
Use the file selector to open a file
Similar to saving files with the file selector, the open file logic is similar. In this example, a Button named btnOpenFile and a TextBlock named tBlockOpenInfo are also defined in XAML.
Private async void btnOpenFile_Click (object sender, RoutedEventArgs e) {FileOpenPicker openFile = new FileOpenPicker (); openFile. suggestedStartLocation = PickerLocationId. documentsLibrary; openFile. viewMode = PickerViewMode. list; openFile. fileTypeFilter. add (". txt "); openFile. fileTypeFilter. add (". docx "); openFile. fileTypeFilter. add (". pptx "); // select a single file StorageFile file = await openFile. pickSingleFile Async (); if (file! = Null) {tBlockOpenInfo. Text = "the file you selected is:" + file. Name;} else {tBlockOpenInfo. Text = "the file opening operation is canceled. ";}// Select multiple files // IReadOnlyList <StorageFile> fileList = await openFile. PickMultipleFilesAsync (); // StringBuilder fileOpenInfo = new StringBuilder (); // if (fileList! = Null) // {// foreach (StorageFile f in fileList) // {// fileOpenInfo. append (f. name + "\ n"); // tBlockOpenInfo. text = "the file you selected is:" + "\ n" + fileOpenInfo. toString (); //} // else // {// tBlockOpenInfo. text = "the file opening operation is canceled. ";//}}
I have also listed the code for selecting multiple files. You only need to uncomment them. For attributes such as ViewMode and FileTypeFilter, you should know the name. Focus on practice.
It is also common on the mobile phone. I tried it just now and successfully entered the resource manager, but failed to open the file. It should be because of the preview version. This preview version has been removed from the Office, and it is estimated that the general version of the Office application will be added to the next version.
So far this blog is over. There are a lot of files and data. I have divided it into many sections here. Thank you for your support.