private void InternalSave(bool bForcePrompt)
        {
            // Prompt the user for the file to save to if this is the first save, or the re-prompt is requested.
            if (bForcePrompt || App.documentSaveStream == null)
            {
                var dialog = new WinForms.SaveFileDialog();
                dialog.Filter = "Personal Genome Explorer files|*.PersonalGenomeData|All files (*.*)|*.*";
                if (dialog.ShowDialog() == WinForms.DialogResult.OK)
                {
                    // Close the old save stream.
                    if (App.documentSaveStream != null)
                    {
                        App.documentSaveStream.Close();
                    }

                    // Open the file chosen by the user for writing.
                    App.documentSaveStream = dialog.OpenFile();
                }
            }

            if (App.documentSaveStream != null)
            {
                // Prompt the user for the password to protect the file with if this is the first save, or the re-prompt is requested.
                if (bForcePrompt || App.document.password == "")
                {
                    var passwordWindow = new PasswordWindow();
                    passwordWindow.Owner = this;
                    passwordWindow.ShowDialog();
                    if (passwordWindow.bOkPressed)
                    {
                        App.document.password = passwordWindow.password;
                    }
                    else
                    {
                        // Abort saving if the password prompt was cancelled.
                        App.document.password = "";
                        return;
                    }
                }

                // Clear the file stream the document was last saved to.
                App.documentSaveStream.Seek(0, SeekOrigin.Begin);
                App.documentSaveStream.SetLength(0);

                // Write the genome to the file.
                App.document.Save(App.documentSaveStream);
            }
        }
        private void menuClick_FileOpen(object sender, RoutedEventArgs args)
        {
            bool bSuccessfullyLoaded = false;

            var dialog = new WinForms.OpenFileDialog();

            dialog.Filter = "Personal Genome Explorer files|*.PersonalGenomeData|All files (*.*)|*.*";
            if (dialog.ShowDialog() == WinForms.DialogResult.OK)
            {
                // If we had a file stream open for saving the current document, close it now to ensure that it doesn't prevent us from opening the user's new file.
                if (App.documentSaveStream != null)
                {
                    App.documentSaveStream.Close();
                    App.documentSaveStream = null;
                }

                // Open the chosen file.
                using (var fileStream = dialog.OpenFile())
                {
                    int TryIndex = 0;
                    while (true)
                    {
                        // Seek to the beginning of the file.
                        fileStream.Seek(0, SeekOrigin.Begin);

                        // Try a blank password before prompting the user.
                        string password = "";
                        if (TryIndex > 0)
                        {
                            // Prompt the user for the genome file's password.
                            var passwordWindow = new PasswordWindow();
                            passwordWindow.Owner = this;
                            passwordWindow.ShowDialog();
                            password = passwordWindow.password;
                            if (!passwordWindow.bOkPressed)
                            {
                                // The user cancelled the password dialog, abort the open.
                                break;
                            }
                        }

                        // Try to load the genome from the chosen file.
                        GenomeLoadResult result = IndividualGenomeDatabase.Load(fileStream, password, ref App.document);

                        // If there was an error, display the appropriate dialog.
                        if (result == GenomeLoadResult.IncorrectPassword)
                        {
                            if (TryIndex > 0)
                            {
                                // If the user entered the wrong password, give them another chance to enter it.
                                WinForms.MessageBox.Show("The file cannot be decrypted with that password.", "Incorrect Password");
                            }
                        }
                        else if (result == GenomeLoadResult.UnrecognizedFile)
                        {
                            WinForms.MessageBox.Show("The file doesn't appear to be a valid PersonalGenomeData file.", "Unrecognized file");
                            break;
                        }
                        else
                        {
                            bSuccessfullyLoaded = true;
                            break;
                        }

                        ++TryIndex;
                    }
                    ;
                }
            }

            if (bSuccessfullyLoaded)
            {
                // Reanalyze after loading the data.
                InitAnalysis();
            }
        }