Exemple #1
0
        private void MenuFileLoad_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Title            = "Load file ...";
                ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                ofd.CheckPathExists  = true;
                StringBuilder filters = new StringBuilder();
                {
                    filters.Append(this.Text);
                    filters.Append(" files (");
                    filters.Append("*" + PW_FILE_EXTENSION);
                    filters.Append(")|");
                    filters.Append("*" + PW_FILE_EXTENSION);
                }
                ofd.Filter = filters.ToString();
                // ofd.FilterIndex doesnt need to be set here (default = 1)

                var    result          = ofd.ShowDialog();
                string FileNameAndPath = ofd.FileName;
                bool   ok = FileNameAndPath.EndsWith(PW_FILE_EXTENSION);

                if (result != DialogResult.OK)
                {
                    return;
                }
                else if (!ok)
                {
                    _ = MessageBox.Show(String.Format("Can't load files with the file extension \"{0}\".",
                                                      System.IO.Path.GetExtension(FileNameAndPath)),
                                        "File loading error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else if ((ofd.FileNames.Length > 1))
                {
                    _ = MessageBox.Show("Something went wrong while trying to access the file", "File access error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    DefaultSaveFileNameAndPath = String.Empty;
                    return;
                }

                try
                {
                    data            = PasswordStorage.DeSerialize(FileNameAndPath);
                    dataGrid.Source = data.GetPasswordDataTable();
                }
                catch (Exception)
                {
                    _ = MessageBox.Show("Something went wrong while trying to read the file", "File read error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    DefaultSaveFileNameAndPath = String.Empty;
                    return;
                }

                // File path is valid, so fast saving should be possible
                DefaultSaveFileNameAndPath = FileNameAndPath;
                justSaved            = true;
                MenuFileSave.Enabled = !justSaved;
            }
        }
Exemple #2
0
 private void RefreshDataGridView()
 {
     this.dataGrid.Source = data.GetPasswordDataTable();
     this.dataGrid.StickToParentBoundaries();
     this.dataGrid.Refresh();
 }