Пример #1
0
        private void OpenFile()
        {
            using (var openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter           = Resources.supportedfiles_opendialogExtension;
                openFileDialog.FilterIndex      = 1;
                openFileDialog.RestoreDirectory = true;

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    if (Regex.IsMatch(Path.GetExtension(openFileDialog.FileName)?.ToLower(), @"^.*\.(xml|xlsx|xls|csv)$"))
                    {
                        Stream openedDocument = null;
                        try
                        {
                            // Load the contents of the file into dataGrid
                            openedDocument      = openFileDialog.OpenFile();
                            dataGrid.DataSource = (Path.GetExtension(openFileDialog.FileName)?.ToLower() == ".xml")
                                ? ImpExpUtilities.GetXmlData(openedDocument).Tables[0]                        // if it's an xml document
                                : ImpExpUtilities.GetSpreadSheetData(openedDocument as FileStream).Tables[0]; // else if (xlsx|xls|csv)
                            EnableCtrl(true);

                            currentOpenDocumentPath = openFileDialog.FileName;
                            this.Text = Resources.title + '[' + openFileDialog.SafeFileName + ']'; // Change the Forms title to currentOpenDoc #10
                        }
                        catch (Exception)
                        {
                            MessageBox.Show(Resources.OpenDoc_fail_msg, Resources.fail,
                                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        finally
                        {
                            openedDocument?.Close();
                        }
                    }
                    else
                    {
                        MessageBox.Show(Resources.OpenDoc_fail_msg, Resources.fail, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }
Пример #2
0
        private void XmlDataGrid_DragDrop(object sender, DragEventArgs e)
        {
            var droppedDocumentPath = (string[])e.Data.GetData(DataFormats.FileDrop, false);

            if (droppedDocumentPath.Length > 1)
            {
                MessageBox.Show(Resources.DragDrop_many_msg, Resources.warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            if (Regex.IsMatch(Path.GetExtension(droppedDocumentPath[0])?.ToLower(), @"^.*\.(xml|xlsx|xls|csv)$"))
            {
                FileStream droppedDocument = null;
                try
                {
                    // Load the contents of the file into dataGrid
                    droppedDocument     = new FileStream(droppedDocumentPath[0], FileMode.Open, FileAccess.Read);                                       // [0] => the first selected file
                    dataGrid.DataSource = (Path.GetExtension(droppedDocumentPath[0])?.ToLower() == ".xml")
                        ? ImpExpUtilities.GetXmlData(droppedDocument).Tables[0]                                                                         // if it's an xml document
                        : ImpExpUtilities.GetSpreadSheetData(droppedDocument).Tables[0];                                                                // else if (xlsx|xls|csv)
                    EnableCtrl(true);
                    currentOpenDocumentPath = (Path.GetExtension(droppedDocumentPath[0])?.ToLower() == ".xml") ? droppedDocumentPath[0] : String.Empty; // Set the currentOpenPath variable to be used later for saving
                    this.Text = Resources.title + '[' + Path.GetFileName(droppedDocumentPath[0]) + ']';                                                 // Change the Forms title to currentOpenDoc #10
                }
                catch (Exception)
                {
                    MessageBox.Show(Resources.OpenDoc_fail_msg, Resources.fail, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    // Fixes #29 file lock issue
                    droppedDocument?.Close();
                }
            }
            else
            {
                MessageBox.Show(Resources.DragDrop_wrongExt_msg, Resources.warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }