示例#1
0
        //---------------------------------------------------
        //
        // Internal Methods
        //
        //---------------------------------------------------
        #region Internal Methods

        /// <summary>
        ///  Performs the actual call to display a file open dialog.
        /// </summary>
        /// <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>
        /// <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 GetOpenFileName and process the result code.
        /// </remarks>
        internal override bool RunFileDialog(NativeMethods.OPENFILENAME_I ofn)
        {
            bool result = false;

            // Make the actual call to GetOpenFileName.  This function
            // blocks on GetOpenFileName 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.GetOpenFileName(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_NOHINSTANCE
                     * CDERR_INITIALIZATION
                     * CDERR_NOHOOK
                     * CDERR_LOCKRESFAILURE
                     * CDERR_NOTEMPLATE
                     * CDERR_LOADRESFAILURE
                     * CDERR_STRUCTSIZE
                     * CDERR_LOADSTRFAILURE
                     * CDERR_MEMALLOCFAILURE
                     * CDERR_MEMLOCKFAILURE
                     */
                }
            }
            return(result);
        }
示例#2
0
        internal override bool RunFileDialog(NativeMethods.OPENFILENAME_I ofn)
        {
            bool openFileName = UnsafeNativeMethods.GetOpenFileName(ofn);

            if (!openFileName)
            {
                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(openFileName);
        }