Ejemplo n.º 1
0
        /// <summary>
        /// Opens an existing file with table data
        /// </summary>
        private void Open()
        {
            SaveIfNecessary();

            OpenFileDialog dialog = new OpenFileDialog
            {
                RestoreDirectory = true,
                DefaultExt       = FILE_EXT,
                FileName         = FILE_NAME,
                Filter           = String.Format("TimeTracker files (*.{0})|*.{0}|All files (*.*)|*.*", FILE_EXT),

                // use directory with "current" file if available
                InitialDirectory = file == null?Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) : file.DirectoryName
            };

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                file = new FileInfo(dialog.FileName);
            }

            if (file != null)
            {
                if (!file.Exists)
                {
                    MessageBox.Show(this, Resources.Application_nonexistentFileMessageBox_Message,
                                    Resources.Application_nonexistentFileMessageBox_Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                StreamReader fs = null;
                try
                {
                    Data.Clear();
                    fs = file.OpenText();

                    string line;
                    while ((line = fs.ReadLine()) != null)
                    {
                        TimeTrackerData value = DataSerializer.DeserializeValue(line, CATEGORY_MAXLENGTH);
                        Data.Add(value);
                    }
                }
                catch (DeserializationException)
                {
                    MessageBox.Show(this, Resources.Application_fileErrorMessageBox_Message,
                                    Resources.Application_fileErrorMessageBox_Caption, MessageBoxButtons.OK, MessageBoxIcon.Stop);

                    file = null;

                    return;
                }
                catch (Exception)
                {
                    MessageBox.Show(this, Resources.Application_fileErrorMessageBox_Message,
                                    Resources.Application_fileErrorMessageBox_Caption, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    return;
                }
                finally
                {
                    fs.Close();
                }
                isSaved = true;
            }

            RefreshFileButtons();
            RefreshTitle();
            RefreshCategoryPicker();
        }