コード例 #1
0
        /// <summary>
        /// Save file.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public bool SaveDocument(SaveType type)
        {
            // Get the file name
            string newFileName = this.fileName;

            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter = fileDlgFilter;

            if ((type == SaveType.SaveAs) ||
                Empty(newFileName))
            {
                if (!Empty(newFileName))
                {
                    saveFileDialog1.InitialDirectory = Path.GetDirectoryName(newFileName);
                    saveFileDialog1.FileName         = Path.GetFileName(newFileName);
                }
                else
                {
                    saveFileDialog1.InitialDirectory = fileDlgInitDir;
                    saveFileDialog1.FileName         = newDocName;
                }

                DialogResult res = saveFileDialog1.ShowDialog(frmOwner);

                if (res != DialogResult.OK)
                {
                    return(false);
                }

                newFileName    = saveFileDialog1.FileName;
                fileDlgInitDir = new FileInfo(newFileName).DirectoryName;
            }

            // Write the data
            try
            {
                using (Stream stream = new FileStream(
                           newFileName, FileMode.Create, FileAccess.Write))
                {
                    // Serialize object to text format
                    IFormatter formatter = new BinaryFormatter();

                    if (SaveEvent != null)          // if caller subscribed to this event
                    {
                        SerializationEventArgs args = new SerializationEventArgs(
                            formatter, stream, newFileName);

                        // raise event
                        SaveEvent(this, args);

                        if (args.Error)
                        {
                            return(false);
                        }
                    }
                }
            }
            catch (ArgumentNullException ex) { return(HandleSaveException(ex, newFileName)); }
            catch (ArgumentOutOfRangeException ex) { return(HandleSaveException(ex, newFileName)); }
            catch (ArgumentException ex) { return(HandleSaveException(ex, newFileName)); }
            catch (SecurityException ex) { return(HandleSaveException(ex, newFileName)); }
            catch (FileNotFoundException ex) { return(HandleSaveException(ex, newFileName)); }
            catch (DirectoryNotFoundException ex) { return(HandleSaveException(ex, newFileName)); }
            catch (PathTooLongException ex) { return(HandleSaveException(ex, newFileName)); }
            catch (IOException ex) { return(HandleSaveException(ex, newFileName)); }

            // Clear the dirty bit, cache the new file name
            // and the caption is set automatically
            Dirty = false;
            SetFileName(newFileName);

            // Success
            return(true);
        }
コード例 #2
0
        /// <summary>
        /// Open document
        /// </summary>
        /// <param name="newFileName">
        /// Document file name. Empty - function shows Open File dialog.
        /// </param>
        /// <returns></returns>
        public bool OpenDocument(string newFileName)
        {
            // Check if we can close current file
            if (!CloseDocument())
            {
                return(false);
            }

            // Get the file to open
            if (Empty(newFileName))
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.Filter           = fileDlgFilter;
                openFileDialog1.InitialDirectory = fileDlgInitDir;

                DialogResult res = openFileDialog1.ShowDialog(frmOwner);

                if (res != DialogResult.OK)
                {
                    return(false);
                }

                newFileName    = openFileDialog1.FileName;
                fileDlgInitDir = new FileInfo(newFileName).DirectoryName;
            }

            // Read the data
            try
            {
                using (Stream stream = new FileStream(
                           newFileName, FileMode.Open, FileAccess.Read))
                {
                    // Deserialize object from text format
                    IFormatter formatter = new BinaryFormatter();

                    if (LoadEvent != null)          // if caller subscribed to this event
                    {
                        SerializationEventArgs args = new SerializationEventArgs(
                            formatter, stream, newFileName);

                        // raise event to load document from file
                        LoadEvent(this, args);

                        if (args.Error)
                        {
                            // report failure
                            if (OpenEvent != null)
                            {
                                OpenEvent(this,
                                          new OpenFileEventArgs(newFileName, false));
                            }

                            return(false);
                        }

                        // raise event to show document in the window
                        if (DocChangedEvent != null)
                        {
                            DocChangedEvent(this, new EventArgs());
                        }
                    }
                }
            }
            // Catch all exceptions which may be raised from this code.
            // Caller is responsible to handle all other exceptions
            // in the functions invoked by LoadEvent and DocChangedEvent.
            catch (ArgumentNullException ex) { return(HandleOpenException(ex, newFileName)); }
            catch (ArgumentOutOfRangeException ex) { return(HandleOpenException(ex, newFileName)); }
            catch (ArgumentException ex) { return(HandleOpenException(ex, newFileName)); }
            catch (SecurityException ex) { return(HandleOpenException(ex, newFileName)); }
            catch (FileNotFoundException ex) { return(HandleOpenException(ex, newFileName)); }
            catch (DirectoryNotFoundException ex) { return(HandleOpenException(ex, newFileName)); }
            catch (PathTooLongException ex) { return(HandleOpenException(ex, newFileName)); }
            catch (IOException ex) { return(HandleOpenException(ex, newFileName)); }

            // Clear dirty bit, cache the file name and set the caption
            Dirty = false;
            SetFileName(newFileName);

            if (OpenEvent != null)
            {
                // report success
                OpenEvent(this, new OpenFileEventArgs(newFileName, true));
            }

            // Success
            return(true);
        }