private void FocusInTree(UniversalPackageFile file)
 {
     this.FileTree.SelectedItem = file;
 }
 public WriteStream(UniversalPackageFile file, byte[] contents)
 {
     this.file = file;
     this.Write(contents, 0, contents.Length);
     this.Position = 0;
 }
        private void Content_ExistingFile(object sender, ExecutedRoutedEventArgs e)
        {
            var referenceFile = this.FileTree.SelectedItem;
            var collection    = referenceFile == null ? this.Package.Files : referenceFile.Collection;
            var prefix        = referenceFile == null ? string.Empty : referenceFile.FullName.Substring(0, referenceFile.FullName.LastIndexOf('/') + 1);

            var dialog = new OpenFileDialog
            {
                CheckFileExists = true,
                Multiselect     = true,
                ValidateNames   = true,
                Filter          = "All files (*.*)|*.*"
            };

            if (dialog.ShowDialog(this) == true)
            {
                Task.Run(() => this.PerformOperationAsync(async() =>
                {
                    UniversalPackageFile firstFile = null;

                    foreach (var fileName in dialog.FileNames)
                    {
                        UniversalPackageFile file;
                        try
                        {
                            file = await collection.CreateFileAsync(prefix + Path.GetFileName(fileName));
                        }
                        catch (ArgumentException)
                        {
                            var name = await this.Dispatcher.InvokeAsync(() =>
                            {
                                var prompt = new NamePromptWindow("Existing File", "New file name:", NamePromptWindow.CreateNameValidator(false, collection, collection == this.Package.Metadata, prefix))
                                {
                                    Text  = Path.GetFileName(fileName),
                                    Owner = this
                                };

                                if (prompt.ShowDialog() == true)
                                {
                                    return(prompt.Text);
                                }

                                return(null);
                            });

                            if (name == null)
                            {
                                continue;
                            }

                            file = await collection.CreateFileAsync(prefix + name);
                        }

                        if (firstFile == null)
                        {
                            firstFile = file;
                        }

                        using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.Asynchronous | FileOptions.SequentialScan))
                        {
                            await file.CopyFromAsync(stream);
                        }
                    }

                    return(firstFile);
                }, firstFile =>
                {
                    if (firstFile != null)
                    {
                        this.FocusInTree(firstFile);
                    }
                }));
            }
        }