From 477c2dd7dca3b61d48b1d3e9480018f569d3c9ee Mon Sep 17 00:00:00 2001 From: duke7553 Date: Mon, 20 May 2019 19:19:58 -0400 Subject: [PATCH] Handle FileNotFound Exception --- Files UWP/Interacts/Interaction.cs | 329 +++++++++++++++-------------- 1 file changed, 169 insertions(+), 160 deletions(-) diff --git a/Files UWP/Interacts/Interaction.cs b/Files UWP/Interacts/Interaction.cs index 8f42b2a6fcd0..1dbc55af04af 100644 --- a/Files UWP/Interacts/Interaction.cs +++ b/Files UWP/Interacts/Interaction.cs @@ -35,192 +35,201 @@ public Interaction(PageType contentPageInstance) public async void List_ItemClick(object sender, DoubleTappedRoutedEventArgs e) { - - if (typeof(PageType) == typeof(GenericFileBrowser)) + try { - var CurrentInstance = ItemViewModel.GetCurrentSelectedTabInstance(); - var index = (type as GenericFileBrowser).data.SelectedIndex; - if (index > -1) + if (typeof(PageType) == typeof(GenericFileBrowser)) { - var clickedOnItem = (type as GenericFileBrowser).instanceViewModel.FilesAndFolders[index]; - // Access MRU List - var mostRecentlyUsed = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList; - - if (clickedOnItem.FileType == "Folder") + var CurrentInstance = ItemViewModel.GetCurrentSelectedTabInstance(); + var index = (type as GenericFileBrowser).data.SelectedIndex; + if (index > -1) { - // Add location to MRU List - mostRecentlyUsed.Add(await StorageFolder.GetFolderFromPathAsync(clickedOnItem.FilePath)); - - var TabInstance = CurrentInstance; - (type as GenericFileBrowser).instanceViewModel.Universal.path = clickedOnItem.FilePath; - TabInstance.PathText.Text = clickedOnItem.FilePath; - TabInstance.TextState.isVisible = Visibility.Collapsed; - TabInstance.FS.isEnabled = false; - (type as GenericFileBrowser).instanceViewModel.CancelLoadAndClearFiles(); - if (clickedOnItem.FilePath == Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)) - { - TabInstance.PathText.Text = "Desktop"; - TabInstance.locationsList.SelectedIndex = 1; - TabInstance.accessibleContentFrame.Navigate(typeof(GenericFileBrowser), YourHome.DesktopPath, new SuppressNavigationTransitionInfo()); - - } - else if (clickedOnItem.FilePath == Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)) - { - TabInstance.PathText.Text = "Documents"; - TabInstance.locationsList.SelectedIndex = 3; - TabInstance.accessibleContentFrame.Navigate(typeof(GenericFileBrowser), YourHome.DocumentsPath, new SuppressNavigationTransitionInfo()); - } - else if (clickedOnItem.FilePath == (Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Downloads")) - { - TabInstance.PathText.Text = "Downloads"; - TabInstance.locationsList.SelectedIndex = 2; - TabInstance.accessibleContentFrame.Navigate(typeof(GenericFileBrowser), YourHome.DownloadsPath, new SuppressNavigationTransitionInfo()); - } - else if (clickedOnItem.FilePath == Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)) - { - TabInstance.locationsList.SelectedIndex = 4; - TabInstance.accessibleContentFrame.Navigate(typeof(GenericFileBrowser), YourHome.PicturesPath, new SuppressNavigationTransitionInfo()); - TabInstance.PathText.Text = "Pictures"; - } - else if (clickedOnItem.FilePath == Environment.GetFolderPath(Environment.SpecialFolder.MyMusic)) - { - TabInstance.PathText.Text = "Music"; - TabInstance.locationsList.SelectedIndex = 5; - TabInstance.accessibleContentFrame.Navigate(typeof(GenericFileBrowser), YourHome.MusicPath, new SuppressNavigationTransitionInfo()); - } - else if (clickedOnItem.FilePath == (Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\OneDrive")) + var clickedOnItem = (type as GenericFileBrowser).instanceViewModel.FilesAndFolders[index]; + // Access MRU List + var mostRecentlyUsed = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList; + + if (clickedOnItem.FileType == "Folder") { - TabInstance.PathText.Text = "OneDrive"; - TabInstance.drivesList.SelectedIndex = 1; - TabInstance.accessibleContentFrame.Navigate(typeof(GenericFileBrowser), YourHome.OneDrivePath, new SuppressNavigationTransitionInfo()); + // Add location to MRU List + mostRecentlyUsed.Add(await StorageFolder.GetFolderFromPathAsync(clickedOnItem.FilePath)); + + var TabInstance = CurrentInstance; + (type as GenericFileBrowser).instanceViewModel.Universal.path = clickedOnItem.FilePath; + TabInstance.PathText.Text = clickedOnItem.FilePath; + TabInstance.TextState.isVisible = Visibility.Collapsed; + TabInstance.FS.isEnabled = false; + (type as GenericFileBrowser).instanceViewModel.CancelLoadAndClearFiles(); + if (clickedOnItem.FilePath == Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)) + { + TabInstance.PathText.Text = "Desktop"; + TabInstance.locationsList.SelectedIndex = 1; + TabInstance.accessibleContentFrame.Navigate(typeof(GenericFileBrowser), YourHome.DesktopPath, new SuppressNavigationTransitionInfo()); + + } + else if (clickedOnItem.FilePath == Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)) + { + TabInstance.PathText.Text = "Documents"; + TabInstance.locationsList.SelectedIndex = 3; + TabInstance.accessibleContentFrame.Navigate(typeof(GenericFileBrowser), YourHome.DocumentsPath, new SuppressNavigationTransitionInfo()); + } + else if (clickedOnItem.FilePath == (Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Downloads")) + { + TabInstance.PathText.Text = "Downloads"; + TabInstance.locationsList.SelectedIndex = 2; + TabInstance.accessibleContentFrame.Navigate(typeof(GenericFileBrowser), YourHome.DownloadsPath, new SuppressNavigationTransitionInfo()); + } + else if (clickedOnItem.FilePath == Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)) + { + TabInstance.locationsList.SelectedIndex = 4; + TabInstance.accessibleContentFrame.Navigate(typeof(GenericFileBrowser), YourHome.PicturesPath, new SuppressNavigationTransitionInfo()); + TabInstance.PathText.Text = "Pictures"; + } + else if (clickedOnItem.FilePath == Environment.GetFolderPath(Environment.SpecialFolder.MyMusic)) + { + TabInstance.PathText.Text = "Music"; + TabInstance.locationsList.SelectedIndex = 5; + TabInstance.accessibleContentFrame.Navigate(typeof(GenericFileBrowser), YourHome.MusicPath, new SuppressNavigationTransitionInfo()); + } + else if (clickedOnItem.FilePath == (Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\OneDrive")) + { + TabInstance.PathText.Text = "OneDrive"; + TabInstance.drivesList.SelectedIndex = 1; + TabInstance.accessibleContentFrame.Navigate(typeof(GenericFileBrowser), YourHome.OneDrivePath, new SuppressNavigationTransitionInfo()); + } + else if (clickedOnItem.FilePath == Environment.GetFolderPath(Environment.SpecialFolder.MyVideos)) + { + TabInstance.PathText.Text = "Videos"; + TabInstance.locationsList.SelectedIndex = 6; + TabInstance.accessibleContentFrame.Navigate(typeof(GenericFileBrowser), YourHome.VideosPath, new SuppressNavigationTransitionInfo()); + } + else + { + if (clickedOnItem.FilePath.Split(@"\")[0].Contains("C:")) + { + TabInstance.drivesList.SelectedIndex = 0; + } + (type as GenericFileBrowser).instanceViewModel.Universal.path = clickedOnItem.FilePath; + TabInstance.accessibleContentFrame.Navigate(typeof(GenericFileBrowser), (type as GenericFileBrowser).instanceViewModel.Universal.path, new SuppressNavigationTransitionInfo()); + } } - else if (clickedOnItem.FilePath == Environment.GetFolderPath(Environment.SpecialFolder.MyVideos)) + else if (clickedOnItem.FileType == "Application") { - TabInstance.PathText.Text = "Videos"; - TabInstance.locationsList.SelectedIndex = 6; - TabInstance.accessibleContentFrame.Navigate(typeof(GenericFileBrowser), YourHome.VideosPath, new SuppressNavigationTransitionInfo()); + // Add location to MRU List + mostRecentlyUsed.Add(await StorageFile.GetFileFromPathAsync(clickedOnItem.FilePath)); + await LaunchExe(clickedOnItem.FilePath); } else { - if (clickedOnItem.FilePath.Split(@"\")[0].Contains("C:")) + StorageFile file = await StorageFile.GetFileFromPathAsync(clickedOnItem.FilePath); + // Add location to MRU List + mostRecentlyUsed.Add(file); + var options = new LauncherOptions { - TabInstance.drivesList.SelectedIndex = 0; - } - (type as GenericFileBrowser).instanceViewModel.Universal.path = clickedOnItem.FilePath; - TabInstance.accessibleContentFrame.Navigate(typeof(GenericFileBrowser), (type as GenericFileBrowser).instanceViewModel.Universal.path, new SuppressNavigationTransitionInfo()); - } - } - else if (clickedOnItem.FileType == "Application") - { - // Add location to MRU List - mostRecentlyUsed.Add(await StorageFile.GetFileFromPathAsync(clickedOnItem.FilePath)); - await LaunchExe(clickedOnItem.FilePath); - } - else - { - StorageFile file = await StorageFile.GetFileFromPathAsync(clickedOnItem.FilePath); - // Add location to MRU List - mostRecentlyUsed.Add(file); - var options = new LauncherOptions - { - DisplayApplicationPicker = false + DisplayApplicationPicker = false - }; - await Launcher.LaunchFileAsync(file, options); + }; + await Launcher.LaunchFileAsync(file, options); + } } } - } - else if (typeof(PageType) == typeof(PhotoAlbum)) - { - var index = (type as PhotoAlbum).gv.SelectedIndex; - var CurrentInstance = ItemViewModel.GetCurrentSelectedTabInstance(); - if (index > -1) + else if (typeof(PageType) == typeof(PhotoAlbum)) { - var clickedOnItem = (type as PhotoAlbum).instanceViewModel.FilesAndFolders[index]; - // Access MRU List - var mostRecentlyUsed = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList; - - if (clickedOnItem.FileType == "Folder") + var index = (type as PhotoAlbum).gv.SelectedIndex; + var CurrentInstance = ItemViewModel.GetCurrentSelectedTabInstance(); + if (index > -1) { - // Add location to MRU List - mostRecentlyUsed.Add(await StorageFolder.GetFolderFromPathAsync(clickedOnItem.FilePath)); - - var TabInstance = CurrentInstance; - (type as PhotoAlbum).instanceViewModel.Universal.path = clickedOnItem.FilePath; - TabInstance.PathText.Text = clickedOnItem.FilePath; - TabInstance.TextState.isVisible = Visibility.Collapsed; - TabInstance.FS.isEnabled = false; - (type as PhotoAlbum).instanceViewModel.CancelLoadAndClearFiles(); - if (clickedOnItem.FilePath == Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)) - { - TabInstance.PathText.Text = "Desktop"; - TabInstance.locationsList.SelectedIndex = 1; - TabInstance.accessibleContentFrame.Navigate(typeof(PhotoAlbum), YourHome.DesktopPath, new SuppressNavigationTransitionInfo()); - } - else if (clickedOnItem.FilePath == Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)) - { - TabInstance.PathText.Text = "Documents"; - TabInstance.locationsList.SelectedIndex = 3; - TabInstance.accessibleContentFrame.Navigate(typeof(PhotoAlbum), YourHome.DocumentsPath, new SuppressNavigationTransitionInfo()); - } - else if (clickedOnItem.FilePath == (Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Downloads")) - { - TabInstance.PathText.Text = "Downloads"; - TabInstance.locationsList.SelectedIndex = 2; - TabInstance.accessibleContentFrame.Navigate(typeof(PhotoAlbum), YourHome.DownloadsPath, new SuppressNavigationTransitionInfo()); - } - else if (clickedOnItem.FilePath == Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)) - { - TabInstance.accessibleContentFrame.Navigate(typeof(PhotoAlbum), YourHome.PicturesPath, new SuppressNavigationTransitionInfo()); - TabInstance.locationsList.SelectedIndex = 4; - TabInstance.PathText.Text = "Pictures"; - } - else if (clickedOnItem.FilePath == Environment.GetFolderPath(Environment.SpecialFolder.MyMusic)) - { - TabInstance.PathText.Text = "Music"; - TabInstance.locationsList.SelectedIndex = 5; - TabInstance.accessibleContentFrame.Navigate(typeof(PhotoAlbum), YourHome.MusicPath, new SuppressNavigationTransitionInfo()); - } - else if (clickedOnItem.FilePath == (Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\OneDrive")) + var clickedOnItem = (type as PhotoAlbum).instanceViewModel.FilesAndFolders[index]; + // Access MRU List + var mostRecentlyUsed = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList; + + if (clickedOnItem.FileType == "Folder") { - TabInstance.PathText.Text = "OneDrive"; - TabInstance.drivesList.SelectedIndex = 1; - TabInstance.accessibleContentFrame.Navigate(typeof(PhotoAlbum), YourHome.OneDrivePath, new SuppressNavigationTransitionInfo()); + // Add location to MRU List + mostRecentlyUsed.Add(await StorageFolder.GetFolderFromPathAsync(clickedOnItem.FilePath)); + + var TabInstance = CurrentInstance; + (type as PhotoAlbum).instanceViewModel.Universal.path = clickedOnItem.FilePath; + TabInstance.PathText.Text = clickedOnItem.FilePath; + TabInstance.TextState.isVisible = Visibility.Collapsed; + TabInstance.FS.isEnabled = false; + (type as PhotoAlbum).instanceViewModel.CancelLoadAndClearFiles(); + if (clickedOnItem.FilePath == Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)) + { + TabInstance.PathText.Text = "Desktop"; + TabInstance.locationsList.SelectedIndex = 1; + TabInstance.accessibleContentFrame.Navigate(typeof(PhotoAlbum), YourHome.DesktopPath, new SuppressNavigationTransitionInfo()); + } + else if (clickedOnItem.FilePath == Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)) + { + TabInstance.PathText.Text = "Documents"; + TabInstance.locationsList.SelectedIndex = 3; + TabInstance.accessibleContentFrame.Navigate(typeof(PhotoAlbum), YourHome.DocumentsPath, new SuppressNavigationTransitionInfo()); + } + else if (clickedOnItem.FilePath == (Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Downloads")) + { + TabInstance.PathText.Text = "Downloads"; + TabInstance.locationsList.SelectedIndex = 2; + TabInstance.accessibleContentFrame.Navigate(typeof(PhotoAlbum), YourHome.DownloadsPath, new SuppressNavigationTransitionInfo()); + } + else if (clickedOnItem.FilePath == Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)) + { + TabInstance.accessibleContentFrame.Navigate(typeof(PhotoAlbum), YourHome.PicturesPath, new SuppressNavigationTransitionInfo()); + TabInstance.locationsList.SelectedIndex = 4; + TabInstance.PathText.Text = "Pictures"; + } + else if (clickedOnItem.FilePath == Environment.GetFolderPath(Environment.SpecialFolder.MyMusic)) + { + TabInstance.PathText.Text = "Music"; + TabInstance.locationsList.SelectedIndex = 5; + TabInstance.accessibleContentFrame.Navigate(typeof(PhotoAlbum), YourHome.MusicPath, new SuppressNavigationTransitionInfo()); + } + else if (clickedOnItem.FilePath == (Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\OneDrive")) + { + TabInstance.PathText.Text = "OneDrive"; + TabInstance.drivesList.SelectedIndex = 1; + TabInstance.accessibleContentFrame.Navigate(typeof(PhotoAlbum), YourHome.OneDrivePath, new SuppressNavigationTransitionInfo()); + } + else if (clickedOnItem.FilePath == Environment.GetFolderPath(Environment.SpecialFolder.MyVideos)) + { + TabInstance.PathText.Text = "Videos"; + TabInstance.drivesList.SelectedIndex = 6; + TabInstance.accessibleContentFrame.Navigate(typeof(PhotoAlbum), YourHome.VideosPath, new SuppressNavigationTransitionInfo()); + } + else + { + TabInstance.drivesList.SelectedIndex = 0; + TabInstance.PathText.Text = clickedOnItem.FilePath; + TabInstance.accessibleContentFrame.Navigate(typeof(PhotoAlbum), clickedOnItem.FilePath, new SuppressNavigationTransitionInfo()); + } } - else if (clickedOnItem.FilePath == Environment.GetFolderPath(Environment.SpecialFolder.MyVideos)) + else if (clickedOnItem.FileType == "Application") { - TabInstance.PathText.Text = "Videos"; - TabInstance.drivesList.SelectedIndex = 6; - TabInstance.accessibleContentFrame.Navigate(typeof(PhotoAlbum), YourHome.VideosPath, new SuppressNavigationTransitionInfo()); + // Add location to MRU List + mostRecentlyUsed.Add(await StorageFile.GetFileFromPathAsync(clickedOnItem.FilePath)); + await LaunchExe(clickedOnItem.FilePath); } else { - TabInstance.drivesList.SelectedIndex = 0; - TabInstance.PathText.Text = clickedOnItem.FilePath; - TabInstance.accessibleContentFrame.Navigate(typeof(PhotoAlbum), clickedOnItem.FilePath, new SuppressNavigationTransitionInfo()); + StorageFile file = await StorageFile.GetFileFromPathAsync(clickedOnItem.FilePath); + // Add location to MRU List + mostRecentlyUsed.Add(file); + var options = new LauncherOptions + { + DisplayApplicationPicker = false + + }; + await Launcher.LaunchFileAsync(file, options); } } - else if (clickedOnItem.FileType == "Application") - { - // Add location to MRU List - mostRecentlyUsed.Add(await StorageFile.GetFileFromPathAsync(clickedOnItem.FilePath)); - await LaunchExe(clickedOnItem.FilePath); - } - else - { - StorageFile file = await StorageFile.GetFileFromPathAsync(clickedOnItem.FilePath); - // Add location to MRU List - mostRecentlyUsed.Add(file); - var options = new LauncherOptions - { - DisplayApplicationPicker = false - }; - await Launcher.LaunchFileAsync(file, options); - } } - } + catch (FileNotFoundException) + { + MessageDialog dialog = new MessageDialog("The file you are attempting to access may have been moved or deleted.", "File Not Found"); + await dialog.ShowAsync(); + NavigationActions.Refresh_Click(null, null); + } + }