Exemple #1
0
        public static async Task <FilesystemResult <BaseStorageFile> > Create(this ShellNewEntry shellEntry, BaseStorageFolder parentFolder, string fileName)
        {
            FilesystemResult <BaseStorageFile> createdFile = null;

            if (!fileName.EndsWith(shellEntry.Extension))
            {
                fileName += shellEntry.Extension;
            }
            if (shellEntry.Template == null)
            {
                createdFile = await FilesystemTasks.Wrap(() => parentFolder.CreateFileAsync(fileName, CreationCollisionOption.GenerateUniqueName).AsTask());
            }
            else
            {
                createdFile = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(shellEntry.Template))
                              .OnSuccess(t => t.CopyAsync(parentFolder, fileName, NameCollisionOption.GenerateUniqueName).AsTask());
            }
            if (createdFile)
            {
                if (shellEntry.Data != null)
                {
                    //await FileIO.WriteBytesAsync(createdFile.Result, this.Data); // Calls unsupported OpenTransactedWriteAsync
                    using (var fileStream = await createdFile.Result.OpenStreamForWriteAsync())
                    {
                        await fileStream.WriteAsync(shellEntry.Data, 0, shellEntry.Data.Length);

                        await fileStream.FlushAsync();
                    }
                }
            }
            return(createdFile);
        }
Exemple #2
0
        public static async Task <FilesystemResult <BaseStorageFile> > Create(this ShellNewEntry shellEntry, string filePath, IShellPage associatedInstance)
        {
            var parentFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(filePath));

            if (parentFolder)
            {
                return(await Create(shellEntry, parentFolder, Path.GetFileName(filePath)));
            }
            return(new FilesystemResult <BaseStorageFile>(null, parentFolder.ErrorCode));
        }
Exemple #3
0
        private static async Task <ShellNewEntry> ParseShellNewRegistryEntry(RegistryKey key, RegistryKey root)
        {
            if (!key.GetValueNames().Contains("NullFile") &&
                !key.GetValueNames().Contains("ItemName") &&
                !key.GetValueNames().Contains("FileName"))
            {
                return(null);
            }

            var extension = root.Name.Substring(root.Name.LastIndexOf('\\') + 1);
            var fileName  = (string)key.GetValue("FileName");

            if (!string.IsNullOrEmpty(fileName) && Path.GetExtension(fileName) != extension)
            {
                return(null);
            }

            byte[] data    = null;
            var    dataObj = key.GetValue("Data");

            if (dataObj != null)
            {
                switch (key.GetValueKind("Data"))
                {
                case RegistryValueKind.Binary:
                    data = (byte[])dataObj;
                    break;

                case RegistryValueKind.String:
                    data = UTF8Encoding.UTF8.GetBytes((string)dataObj);
                    break;
                }
            }

            var sampleFile = await FilesystemTasks.Wrap(() => ApplicationData.Current.LocalFolder.CreateFolderAsync("extensions", CreationCollisionOption.OpenIfExists).AsTask())
                             .OnSuccess(t => t.CreateFileAsync("file" + extension, CreationCollisionOption.OpenIfExists).AsTask());

            var displayType = sampleFile ? sampleFile.Result.DisplayType : string.Format("{0} {1}", "file", extension);
            var thumbnail   = sampleFile ? await FilesystemTasks.Wrap(() => sampleFile.Result.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.ListView, 24, Windows.Storage.FileProperties.ThumbnailOptions.UseCurrentScale).AsTask()) : null;

            var entry = new ShellNewEntry()
            {
                Extension = extension,
                Template  = fileName,
                Name      = displayType,
                Command   = (string)key.GetValue("Command"),
                //Name = (string)key.GetValue("ItemName"),
                //IconPath = (string)key.GetValue("IconPath"),
                Icon = thumbnail?.Result,
                Data = data
            };

            return(entry);
        }
        public static async Task <FilesystemResult <BaseStorageFile> > Create(this ShellNewEntry shellEntry, BaseStorageFolder parentFolder, string filePath)
        {
            FilesystemResult <BaseStorageFile> createdFile = null;
            var fileName = Path.GetFileName(filePath);

            if (!fileName.EndsWith(shellEntry.Extension, StringComparison.Ordinal))
            {
                fileName += shellEntry.Extension;
            }
            if (shellEntry.Command != null)
            {
                var args = CommandLine.CommandLineParser.SplitArguments(shellEntry.Command);
                if (args.Any())
                {
                    var connection = await AppServiceConnectionHelper.Instance;
                    if (connection != null)
                    {
                        _ = await connection.SendMessageForResponseAsync(new ValueSet()
                        {
                            { "Arguments", "LaunchApp" },
                            { "WorkingDirectory", PathNormalization.GetParentDir(filePath) },
                            { "Application", args[0].Replace("\"", "", StringComparison.Ordinal) },
                            { "Parameters", string.Join(" ", args.Skip(1)).Replace("%1", filePath) }
                        });
                    }
                }
                createdFile = new FilesystemResult <BaseStorageFile>(null, Shared.Enums.FileSystemStatusCode.Success);
            }
            else if (shellEntry.Template == null)
            {
                createdFile = await FilesystemTasks.Wrap(() => parentFolder.CreateFileAsync(fileName, CreationCollisionOption.GenerateUniqueName).AsTask());
            }
            else
            {
                createdFile = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(shellEntry.Template))
                              .OnSuccess(t => t.CopyAsync(parentFolder, fileName, NameCollisionOption.GenerateUniqueName).AsTask());
            }
            if (createdFile)
            {
                if (shellEntry.Data != null)
                {
                    //await FileIO.WriteBytesAsync(createdFile.Result, shellEntry.Data); // Calls unsupported OpenTransactedWriteAsync
                    await createdFile.Result.WriteBytesAsync(shellEntry.Data);
                }
            }
            return(createdFile);
        }
        public static async Task <FilesystemResult <BaseStorageFile> > Create(this ShellNewEntry shellEntry, BaseStorageFolder parentFolder, string filePath)
        {
            FilesystemResult <BaseStorageFile> createdFile = null;
            var fileName = Path.GetFileName(filePath);

            if (shellEntry.Template == null)
            {
                createdFile = await FilesystemTasks.Wrap(() => parentFolder.CreateFileAsync(fileName, CreationCollisionOption.GenerateUniqueName).AsTask());
            }
            else
            {
                createdFile = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(shellEntry.Template))
                              .OnSuccess(t => t.CopyAsync(parentFolder, fileName, NameCollisionOption.GenerateUniqueName).AsTask());
            }
            if (createdFile)
            {
                if (shellEntry.Data != null)
                {
                    //await FileIO.WriteBytesAsync(createdFile.Result, shellEntry.Data); // Calls unsupported OpenTransactedWriteAsync
                    await createdFile.Result.WriteBytesAsync(shellEntry.Data);
                }
            }
            return(createdFile);
        }
        public static async Task <IStorageItem> CreateFileFromDialogResultTypeForResult(AddItemType itemType, ShellNewEntry itemInfo, IShellPage associatedInstance)
        {
            string currentPath = null;

            if (associatedInstance.SlimContentPage != null)
            {
                currentPath = associatedInstance.FilesystemViewModel.WorkingDirectory;
                if (App.LibraryManager.TryGetLibrary(currentPath, out var library))
                {
                    if (!library.IsEmpty && library.Folders.Count == 1) // TODO: handle libraries with multiple folders
                    {
                        currentPath = library.Folders.First();
                    }
                }
            }

            // Show rename dialog
            DynamicDialog dialog = DynamicDialogFactory.GetFor_RenameDialog();
            await dialog.ShowAsync();

            if (dialog.DynamicResult != DynamicDialogResult.Primary)
            {
                return(null);
            }

            // Create file based on dialog result
            string userInput = dialog.ViewModel.AdditionalData as string;
            var    folderRes = await associatedInstance.FilesystemViewModel.GetFolderWithPathFromPathAsync(currentPath);

            var created = new FilesystemResult <(ReturnResult, IStorageItem)>((ReturnResult.Failed, null), FileSystemStatusCode.Generic);

            if (folderRes)
            {
                switch (itemType)
                {
                case AddItemType.Folder:
                    userInput = !string.IsNullOrWhiteSpace(userInput) ? userInput : "NewFolder".GetLocalized();
                    created   = await FilesystemTasks.Wrap(async() =>
                    {
                        return(await associatedInstance.FilesystemHelpers.CreateAsync(
                                   StorageHelpers.FromPathAndType(PathNormalization.Combine(folderRes.Result.Path, userInput), FilesystemItemType.Directory),
                                   true));
                    });

                    break;

                case AddItemType.File:
                    userInput = !string.IsNullOrWhiteSpace(userInput) ? userInput : itemInfo?.Name ?? "NewFile".GetLocalized();
                    created   = await FilesystemTasks.Wrap(async() =>
                    {
                        return(await associatedInstance.FilesystemHelpers.CreateAsync(
                                   StorageHelpers.FromPathAndType(PathNormalization.Combine(folderRes.Result.Path, userInput + itemInfo?.Extension), FilesystemItemType.File),
                                   true));
                    });

                    break;
                }
            }

            if (created == FileSystemStatusCode.Unauthorized)
            {
                await DialogDisplayHelper.ShowDialogAsync("AccessDenied".GetLocalized(), "AccessDeniedCreateDialog/Text".GetLocalized());
            }

            return(created.Result.Item2);
        }
 public static async void CreateFileFromDialogResultType(AddItemType itemType, ShellNewEntry itemInfo, IShellPage associatedInstance)
 {
     _ = await CreateFileFromDialogResultTypeForResult(itemType, itemInfo, associatedInstance);
 }
        public static async void CreateFileFromDialogResultType(AddItemType itemType, ShellNewEntry itemInfo, IShellPage associatedInstance)
        {
            string currentPath = null;

            if (associatedInstance.SlimContentPage != null)
            {
                currentPath = associatedInstance.FilesystemViewModel.WorkingDirectory;
            }

            // Show rename dialog
            DynamicDialog dialog = DynamicDialogFactory.GetFor_RenameDialog();
            await dialog.ShowAsync();

            if (dialog.DynamicResult != DynamicDialogResult.Primary)
            {
                return;
            }

            // Create file based on dialog result
            string userInput = dialog.ViewModel.AdditionalData as string;
            var    folderRes = await associatedInstance.FilesystemViewModel.GetFolderWithPathFromPathAsync(currentPath);

            FilesystemResult created = folderRes;

            if (folderRes)
            {
                switch (itemType)
                {
                case AddItemType.Folder:
                    userInput = !string.IsNullOrWhiteSpace(userInput) ? userInput : "NewFolder".GetLocalized();
                    created   = await FilesystemTasks.Wrap(async() =>
                    {
                        return(await associatedInstance.FilesystemHelpers.CreateAsync(
                                   StorageItemHelpers.FromPathAndType(System.IO.Path.Combine(folderRes.Result.Path, userInput), FilesystemItemType.Directory),
                                   true));
                    });

                    break;

                case AddItemType.File:
                    userInput = !string.IsNullOrWhiteSpace(userInput) ? userInput : itemInfo?.Name ?? "NewFile".GetLocalized();
                    created   = await FilesystemTasks.Wrap(async() =>
                    {
                        return(await associatedInstance.FilesystemHelpers.CreateAsync(
                                   StorageItemHelpers.FromPathAndType(System.IO.Path.Combine(folderRes.Result.Path, userInput + itemInfo?.Extension), FilesystemItemType.File),
                                   true));
                    });

                    break;
                }
            }
            if (created == FileSystemStatusCode.Unauthorized)
            {
                await DialogDisplayHelper.ShowDialogAsync("AccessDeniedCreateDialog/Title".GetLocalized(), "AccessDeniedCreateDialog/Text".GetLocalized());
            }
        }
 public virtual void CreateNewFile(ShellNewEntry f)
 {
     UIFilesystemHelpers.CreateFileFromDialogResultType(AddItemType.File, f, associatedInstance);
 }
Exemple #10
0
        private static async Task <ShellNewEntry> ParseShellNewRegistryEntry(RegistryKey key, RegistryKey root)
        {
            var valueNames = key.GetValueNames();

            if (!valueNames.Contains("NullFile") &&
                !valueNames.Contains("ItemName") &&
                !valueNames.Contains("FileName"))
            {
                return(null);
            }

            var extension = root.Name.Substring(root.Name.LastIndexOf('\\') + 1);
            var fileName  = (string)key.GetValue("FileName");

            if (!string.IsNullOrEmpty(fileName) && Path.GetExtension(fileName) != extension)
            {
                return(null);
            }

            byte[] data    = null;
            var    dataObj = key.GetValue("Data");

            if (dataObj != null)
            {
                switch (key.GetValueKind("Data"))
                {
                case RegistryValueKind.Binary:
                    data = (byte[])dataObj;
                    break;

                case RegistryValueKind.String:
                    data = UTF8Encoding.UTF8.GetBytes((string)dataObj);
                    break;
                }
            }

            var folder = await Extensions.IgnoreExceptions(() => ApplicationData.Current.LocalFolder.CreateFolderAsync("extensions", CreationCollisionOption.OpenIfExists).AsTask());

            var sampleFile = folder != null ? await Extensions.IgnoreExceptions(() => folder.CreateFileAsync("file" + extension, CreationCollisionOption.OpenIfExists).AsTask()) : null;

            var displayType = sampleFile != null ? sampleFile.DisplayType : string.Format("{0} {1}", "file", extension);
            var thumbnail   = sampleFile != null ? await Extensions.IgnoreExceptions(() => sampleFile.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.ListView, 24, Windows.Storage.FileProperties.ThumbnailOptions.UseCurrentScale).AsTask()) : null;

            string iconString = null;

            if (thumbnail != null)
            {
                var readStream = thumbnail.AsStreamForRead();
                var bitmapData = new byte[readStream.Length];
                await readStream.ReadAsync(bitmapData, 0, bitmapData.Length);

                iconString = Convert.ToBase64String(bitmapData, 0, bitmapData.Length);
            }

            var entry = new ShellNewEntry()
            {
                Extension  = extension,
                Template   = fileName,
                Name       = displayType,
                Command    = (string)key.GetValue("Command"),
                IconBase64 = iconString,
                Data       = data
            };

            return(entry);
        }
Exemple #11
0
        public static async Task <IStorageItem> CreateFileFromDialogResultTypeForResult(AddItemDialogItemType itemType, ShellNewEntry itemInfo, IShellPage associatedInstance)
        {
            string currentPath = null;

            if (associatedInstance.SlimContentPage != null)
            {
                currentPath = associatedInstance.FilesystemViewModel.WorkingDirectory;
                if (App.LibraryManager.TryGetLibrary(currentPath, out var library))
                {
                    if (!library.IsEmpty && library.Folders.Count == 1) // TODO: handle libraries with multiple folders
                    {
                        currentPath = library.Folders.First();
                    }
                }
            }

            // Skip rename dialog when ShellNewEntry has a Command (e.g. ".accdb", ".gdoc")
            string userInput = null;

            if (itemType != AddItemDialogItemType.File || itemInfo?.Command == null)
            {
                DynamicDialog dialog = DynamicDialogFactory.GetFor_RenameDialog();
                await dialog.ShowAsync(); // Show rename dialog

                if (dialog.DynamicResult != DynamicDialogResult.Primary)
                {
                    return(null);
                }

                userInput = dialog.ViewModel.AdditionalData as string;
            }

            // Create file based on dialog result
            (ReturnResult Status, IStorageItem Item)created = (ReturnResult.Failed, null);
            switch (itemType)
            {
            case AddItemDialogItemType.Folder:
                userInput = !string.IsNullOrWhiteSpace(userInput) ? userInput : "NewFolder".GetLocalized();
                created   = await associatedInstance.FilesystemHelpers.CreateAsync(
                    StorageHelpers.FromPathAndType(PathNormalization.Combine(currentPath, userInput), FilesystemItemType.Directory),
                    true);

                break;

            case AddItemDialogItemType.File:
                userInput = !string.IsNullOrWhiteSpace(userInput) ? userInput : itemInfo?.Name ?? "NewFile".GetLocalized();
                created   = await associatedInstance.FilesystemHelpers.CreateAsync(
                    StorageHelpers.FromPathAndType(PathNormalization.Combine(currentPath, userInput + itemInfo?.Extension), FilesystemItemType.File),
                    true);

                break;
            }

            if (created.Status == ReturnResult.AccessUnauthorized)
            {
                await DialogDisplayHelper.ShowDialogAsync("AccessDenied".GetLocalized(), "AccessDeniedCreateDialog/Text".GetLocalized());
            }

            return(created.Item);
        }