Пример #1
0
        public static async void OpenSelectedItems(IShellPage associatedInstance, bool openViaApplicationPicker = false)
        {
            if (associatedInstance.FilesystemViewModel.WorkingDirectory.StartsWith(CommonPaths.RecycleBinPath, StringComparison.Ordinal))
            {
                // Do not open files and folders inside the recycle bin
                return;
            }
            if (associatedInstance.SlimContentPage == null)
            {
                return;
            }

            bool forceOpenInNewTab = false;
            var  selectedItems     = associatedInstance.SlimContentPage.SelectedItems.ToList();
            var  opened            = false;

            if (!openViaApplicationPicker &&
                selectedItems.Count > 1 &&
                selectedItems.All(x => x.PrimaryItemAttribute == StorageItemTypes.File && !x.IsExecutable && !x.IsShortcutItem))
            {
                // Multiple files are selected, open them together
                opened = await Win32Helpers.InvokeWin32ComponentAsync(string.Join('|', selectedItems.Select(x => x.ItemPath)), associatedInstance);
            }
            if (!opened)
            {
                foreach (ListedItem item in selectedItems)
                {
                    var type = item.PrimaryItemAttribute == StorageItemTypes.Folder ?
                               FilesystemItemType.Directory : FilesystemItemType.File;

                    await OpenPath(item.ItemPath, associatedInstance, type, false, openViaApplicationPicker, forceOpenInNewTab : forceOpenInNewTab);

                    if (type == FilesystemItemType.Directory)
                    {
                        forceOpenInNewTab = true;
                    }
                }
            }
        }
Пример #2
0
        private static async Task <FilesystemResult> OpenFile(string path, IShellPage associatedInstance, IEnumerable <string> selectItems, ShellLinkItem shortcutInfo, bool openViaApplicationPicker = false, string args = default)
        {
            var  opened         = (FilesystemResult)false;
            bool isHiddenItem   = NativeFileOperationsHelper.HasFileAttribute(path, System.IO.FileAttributes.Hidden);
            bool isShortcutItem = path.EndsWith(".lnk", StringComparison.Ordinal) || path.EndsWith(".url", StringComparison.Ordinal) || !string.IsNullOrEmpty(shortcutInfo.TargetPath);

            if (isShortcutItem)
            {
                if (string.IsNullOrEmpty(shortcutInfo.TargetPath))
                {
                    await Win32Helpers.InvokeWin32ComponentAsync(path, associatedInstance, args);
                }
                else
                {
                    if (!path.EndsWith(".url", StringComparison.Ordinal))
                    {
                        StorageFileWithPath childFile = await associatedInstance.FilesystemViewModel.GetFileWithPathFromPathAsync(shortcutInfo.TargetPath);

                        if (childFile != null)
                        {
                            // Add location to Recent Items List
                            if (childFile.Item is SystemStorageFile)
                            {
                                await App.RecentItemsManager.AddToRecentItems(childFile.Path);
                            }
                        }
                    }
                    await Win32Helpers.InvokeWin32ComponentAsync(shortcutInfo.TargetPath, associatedInstance, $"{args} {shortcutInfo.Arguments}", shortcutInfo.RunAsAdmin, shortcutInfo.WorkingDirectory);
                }
                opened = (FilesystemResult)true;
            }
            else if (isHiddenItem)
            {
                await Win32Helpers.InvokeWin32ComponentAsync(path, associatedInstance, args);
            }
            else
            {
                opened = await associatedInstance.FilesystemViewModel.GetFileWithPathFromPathAsync(path)
                         .OnSuccess(async childFile =>
                {
                    // Add location to Recent Items List
                    if (childFile.Item is SystemStorageFile)
                    {
                        await App.RecentItemsManager.AddToRecentItems(childFile.Path);
                    }

                    if (openViaApplicationPicker)
                    {
                        LauncherOptions options = new LauncherOptions
                        {
                            DisplayApplicationPicker = true
                        };
                        if (!await Launcher.LaunchFileAsync(childFile.Item, options))
                        {
                            var connection = await AppServiceConnectionHelper.Instance;
                            if (connection != null)
                            {
                                await connection.SendMessageAsync(new ValueSet()
                                {
                                    { "Arguments", "InvokeVerb" },
                                    { "FilePath", path },
                                    { "Verb", "openas" }
                                });
                            }
                        }
                    }
                    else
                    {
                        //try using launcher first
                        bool launchSuccess = false;

                        BaseStorageFileQueryResult fileQueryResult = null;

                        //Get folder to create a file query (to pass to apps like Photos, Movies & TV..., needed to scroll through the folder like what Windows Explorer does)
                        BaseStorageFolder currentFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(path));

                        if (currentFolder != null)
                        {
                            QueryOptions queryOptions = new QueryOptions(CommonFileQuery.DefaultQuery, null);

                            //We can have many sort entries
                            SortEntry sortEntry = new SortEntry()
                            {
                                AscendingOrder = associatedInstance.InstanceViewModel.FolderSettings.DirectorySortDirection == SortDirection.Ascending
                            };

                            //Basically we tell to the launched app to follow how we sorted the files in the directory.
                            var sortOption = associatedInstance.InstanceViewModel.FolderSettings.DirectorySortOption;

                            switch (sortOption)
                            {
                            case SortOption.Name:
                                sortEntry.PropertyName = "System.ItemNameDisplay";
                                queryOptions.SortOrder.Clear();
                                queryOptions.SortOrder.Add(sortEntry);
                                break;

                            case SortOption.DateModified:
                                sortEntry.PropertyName = "System.DateModified";
                                queryOptions.SortOrder.Clear();
                                queryOptions.SortOrder.Add(sortEntry);
                                break;

                            case SortOption.DateCreated:
                                sortEntry.PropertyName = "System.DateCreated";
                                queryOptions.SortOrder.Clear();
                                queryOptions.SortOrder.Add(sortEntry);
                                break;

                            //Unfortunately this is unsupported | Remarks: https://docs.microsoft.com/en-us/uwp/api/windows.storage.search.queryoptions.sortorder?view=winrt-19041
                            //case Enums.SortOption.Size:

                            //sortEntry.PropertyName = "System.TotalFileSize";
                            //queryOptions.SortOrder.Clear();
                            //queryOptions.SortOrder.Add(sortEntry);
                            //break;

                            //Unfortunately this is unsupported | Remarks: https://docs.microsoft.com/en-us/uwp/api/windows.storage.search.queryoptions.sortorder?view=winrt-19041
                            //case Enums.SortOption.FileType:

                            //sortEntry.PropertyName = "System.FileExtension";
                            //queryOptions.SortOrder.Clear();
                            //queryOptions.SortOrder.Add(sortEntry);
                            //break;

                            //Handle unsupported
                            default:
                                //keep the default one in SortOrder IList
                                break;
                            }

                            var options = new LauncherOptions();
                            if (currentFolder.AreQueryOptionsSupported(queryOptions))
                            {
                                fileQueryResult = currentFolder.CreateFileQueryWithOptions(queryOptions);
                                options.NeighboringFilesQuery = fileQueryResult.ToStorageFileQueryResult();
                            }

                            // Now launch file with options.
                            var storageItem = (StorageFile)await FilesystemTasks.Wrap(() => childFile.Item.ToStorageFileAsync().AsTask());
                            if (storageItem != null)
                            {
                                launchSuccess = await Launcher.LaunchFileAsync(storageItem, options);
                            }
                        }

                        if (!launchSuccess)
                        {
                            await Win32Helpers.InvokeWin32ComponentAsync(path, associatedInstance, args);
                        }
                    }
                });
            }
            return(opened);
        }
Пример #3
0
        private static async Task <FilesystemResult> OpenDirectory(string path, IShellPage associatedInstance, IEnumerable <string> selectItems, ShellLinkItem shortcutInfo, bool forceOpenInNewTab)
        {
            IUserSettingsService userSettingsService = Ioc.Default.GetService <IUserSettingsService>();

            var  opened         = (FilesystemResult)false;
            bool isHiddenItem   = NativeFileOperationsHelper.HasFileAttribute(path, System.IO.FileAttributes.Hidden);
            bool isShortcutItem = path.EndsWith(".lnk", StringComparison.Ordinal) || path.EndsWith(".url", StringComparison.Ordinal); // Determine

            if (isShortcutItem)
            {
                if (string.IsNullOrEmpty(shortcutInfo.TargetPath))
                {
                    await Win32Helpers.InvokeWin32ComponentAsync(path, associatedInstance);

                    opened = (FilesystemResult)true;
                }
                else
                {
                    if (forceOpenInNewTab || userSettingsService.PreferencesSettingsService.OpenFoldersInNewTab)
                    {
                        await OpenPathInNewTab(shortcutInfo.TargetPath);
                    }
                    else
                    {
                        associatedInstance.ToolbarViewModel.PathControlDisplayText = shortcutInfo.TargetPath;
                        associatedInstance.NavigateWithArguments(associatedInstance.InstanceViewModel.FolderSettings.GetLayoutType(shortcutInfo.TargetPath), new NavigationArguments()
                        {
                            NavPathParam          = shortcutInfo.TargetPath,
                            AssociatedTabInstance = associatedInstance,
                            SelectItems           = selectItems
                        });
                    }

                    opened = (FilesystemResult)true;
                }
            }
            else if (isHiddenItem)
            {
                if (forceOpenInNewTab || userSettingsService.PreferencesSettingsService.OpenFoldersInNewTab)
                {
                    await OpenPathInNewTab(path);
                }
                else
                {
                    associatedInstance.ToolbarViewModel.PathControlDisplayText = path;
                    associatedInstance.NavigateWithArguments(associatedInstance.InstanceViewModel.FolderSettings.GetLayoutType(path), new NavigationArguments()
                    {
                        NavPathParam          = path,
                        AssociatedTabInstance = associatedInstance
                    });
                }

                opened = (FilesystemResult)true;
            }
            else
            {
                opened = await associatedInstance.FilesystemViewModel.GetFolderWithPathFromPathAsync(path)
                         .OnSuccess(async(childFolder) =>
                {
                    // Add location to Recent Items List
                    if (childFolder.Item is SystemStorageFolder)
                    {
                        await App.RecentItemsManager.AddToRecentItems(childFolder.Path);
                    }
                });

                if (!opened)
                {
                    opened = (FilesystemResult)FolderHelpers.CheckFolderAccessWithWin32(path);
                }
                if (opened)
                {
                    if (forceOpenInNewTab || userSettingsService.PreferencesSettingsService.OpenFoldersInNewTab)
                    {
                        await OpenPathInNewTab(path);
                    }
                    else
                    {
                        associatedInstance.ToolbarViewModel.PathControlDisplayText = path;
                        associatedInstance.NavigateWithArguments(associatedInstance.InstanceViewModel.FolderSettings.GetLayoutType(path), new NavigationArguments()
                        {
                            NavPathParam          = path,
                            AssociatedTabInstance = associatedInstance,
                            SelectItems           = selectItems
                        });
                    }
                }
                else
                {
                    await Win32Helpers.InvokeWin32ComponentAsync(path, associatedInstance);
                }
            }
            return(opened);
        }