Example #1
0
        /// <summary>
        ///  Performs the actual call to display a file save dialog.
        /// </summary>
        /// <remarks>
        ///  The call chain is ShowDialog > RunDialog >
        ///  RunFileDialog (this function).  In
        ///  FileDialog.RunDialog, we created the OPENFILENAME
        ///  structure - so all this function needs to do is
        ///  call GetSaveFileName and process the result code.
        /// </remarks>
        /// <exception cref="InvalidOperationException">
        /// Thrown if there is an invalid filename, if
        /// a subclass failure occurs or if the buffer length
        /// allocated to store the filenames occurs.
        /// </exception>
        internal override bool RunFileDialog(NativeMethods.OPENFILENAME_I ofn)
        {
            bool result = false;

            // Make the actual call to GetSaveFileName.  This function
            // blocks on GetSaveFileName until the entire dialog display
            // is completed - any interaction we have with the dialog
            // while it's open takes place through our HookProc.  The
            // return value is a bool;  true = success.
            result = UnsafeNativeMethods.GetSaveFileName(ofn);

            if (!result)    // result was 0 (false), so an error occurred.
            {
                // Something may have gone wrong - check for error conditions
                // by calling CommDlgExtendedError to get the specific error.
                int errorCode = UnsafeNativeMethods.CommDlgExtendedError();

                // Throw an appropriate exception if we know what happened:
                switch (errorCode)
                {
                // FNERR_INVALIDFILENAME is usually triggered when an invalid initial filename is specified
                case NativeMethods.FNERR_INVALIDFILENAME:
                    throw new InvalidOperationException(SR.Get(SRID.FileDialogInvalidFileName, SafeFileName));

                case NativeMethods.FNERR_SUBCLASSFAILURE:
                    throw new InvalidOperationException(SR.Get(SRID.FileDialogSubClassFailure));

                // note for FNERR_BUFFERTOOSMALL:
                // This error likely indicates a problem with our buffer size growing code;
                // take a look at that part of HookProc if customers report this error message is occurring.
                case NativeMethods.FNERR_BUFFERTOOSMALL:
                    throw new InvalidOperationException(SR.Get(SRID.FileDialogBufferTooSmall));

                    /*
                     * According to MSDN, the following errors can also occur, but we do not handle them as
                     * they are very unlikely, and if they do occur, they indicate a catastrophic failure.
                     * Most are related to features we do not wrap in our implementation.
                     *
                     * CDERR_DIALOGFAILURE
                     * CDERR_FINDRESFAILURE
                     * CDERR_INITIALIZATION
                     * CDERR_LOADRESFAILURE
                     * CDERR_LOADSTRFAILURE
                     * CDERR_LOCKRESFAILURE
                     * CDERR_MEMALLOCFAILURE
                     * CDERR_MEMLOCKFAILURE
                     * CDERR_NOHINSTANCE
                     * CDERR_NOHOOK
                     * CDERR_NOTEMPLATE
                     * CDERR_STRUCTSIZE
                     */
                }
            }
            return(result);
        }
Example #2
0
        internal override bool RunFileDialog(NativeMethods.OPENFILENAME_I ofn)
        {
            bool saveFileName = UnsafeNativeMethods.GetSaveFileName(ofn);

            if (!saveFileName)
            {
                switch (UnsafeNativeMethods.CommDlgExtendedError())
                {
                case 12289:
                    throw new InvalidOperationException(SR.Get("FileDialogSubClassFailure"));

                case 12290:
                    throw new InvalidOperationException(SR.Get("FileDialogInvalidFileName", new object[]
                    {
                        base.SafeFileName
                    }));

                case 12291:
                    throw new InvalidOperationException(SR.Get("FileDialogBufferTooSmall"));
                }
            }
            return(saveFileName);
        }