window store app 附件读取

  1   public static async Task<bool> DisplayApplicationPicker(string folderName, string fileName)
  2         {
  3             // Path to the file in the app package to launch  
  4            
  5 
  6             MessageDialog message = null;
  7             StorageFolder storageFolder = null;
  8             StorageFile GetStorageFile = null;
  9 
 10             IReadOnlyList<StorageFolder> storageFolders;
 11 
 12             bool isExist = false;
 13             try
 14             {
 15 
 16                 try
 17                 {
 18 
 19                     //在指定的应用程序文件夹下查找指定的文件
 20                     storageFolder = ApplicationData.Current.LocalFolder;
 21                     storageFolders = await storageFolder.GetFoldersAsync();
 22                     isExist = storageFolders.Any(folder => folder.Name == folderName);
 23                     if (isExist)
 24                     {
 25                         storageFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync(folderName);
 26 
 27                     }
 28                     else
 29                     {
 30                         storageFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(folderName);
 31                     }
 32 
 33                 }
 34                 catch (System.IO.FileNotFoundException ex)
 35                 {
 36                     message = new MessageDialog(ex.Message);
 37                     message.ShowAsync();
 38                 }
 39                 //判断是否有该文件
 40                 try
 41                 {
 42                     //在指定的应用程序文件夹下查找指定的文件
 43                     if (isExist)
 44                     {
 45                         IReadOnlyList<StorageFile> files = await storageFolder.GetFilesAsync();
 46                         bool isExistfile = files.Any(file => file.Name == fileName);
 47                         if (!isExistfile)
 48                         {
 49                             message = new MessageDialog("Didn't find the specified file");
 50                             message.ShowAsync();
 51                             //todo: Written to the file Method
 52                         }
 53                         else
 54                         {
 55                             GetStorageFile = await storageFolder.GetFileAsync(fileName);
 56                         }
 57 
 58 
 59                     }
 60 
 61                 }
 62                 catch (System.IO.FileNotFoundException ex)
 63                 {
 64                     message = new MessageDialog(ex.Message);
 65                     message.ShowAsync();
 66                 }
 67 
 68 
 69                 if (GetStorageFile != null)
 70                 {
 71                     // Set the option to show the picker  
 72                     var options = new Windows.System.LauncherOptions();
 73                     options.DisplayApplicationPicker = true;
 74 
 75                     // Launch the retrieved file  
 76                     bool success = await Windows.System.Launcher.LaunchFileAsync(GetStorageFile, options);
 77 
 78                     if (success)
 79                     {
 80                         // File launched 
 81                         return true;
 82 
 83                     }
 84                     else
 85                     {
 86                         // File launch failed  
 87                         message = new MessageDialog("Please choose other open way");
 88                         message.ShowAsync();
 89                         return false;
 90 
 91                     }
 92                 }
 93                 else
 94                 {
 95                     message = new MessageDialog( "Didn't find the specified file");
 96                     message.ShowAsync();
 97                     return false;
 98                 }
 99                 
100 
101             }
102             catch (Exception ex)
103             {
104                 message = new MessageDialog(ex.Message);
105                 message.ShowAsync();
106                 return false;
107             }
108 
109         }
原文地址:https://www.cnblogs.com/Mengyl/p/3741488.html