//for some reason, the API pack doesn't expose this method from the COM interface
        static List <string> GetSelectedItems(CommonFileDialog dialog)
        {
            var             filenames    = new List <string> ();
            var             nativeDialog = (IFileOpenDialog)dialog.nativeDialog;
            IShellItemArray resultsArray;
            uint            count;

            try {
                nativeDialog.GetSelectedItems(out resultsArray);
            } catch (Exception ex) {
                //we get E_FAIL when there is no selection
                var ce = ex.InnerException as COMException;
                if (ce != null && ce.ErrorCode == -2147467259)
                {
                    return(filenames);
                }
                throw;
            }

            var hr = (int)resultsArray.GetCount(out count);

            if (hr != 0)
            {
                throw Marshal.GetExceptionForHR(hr);
            }

            for (int i = 0; i < count; ++i)
            {
                var    item = CommonFileDialog.GetShellItemAt(resultsArray, i);
                string val  = CommonFileDialog.GetFileNameFromShellItem(item);
                filenames.Add(val);
            }

            return(filenames);
        }
예제 #2
0
        //for some reason, the API pack doesn't expose this method from the COM interface
        static List <string> GetSelectedItems(CommonFileDialog dialog)
        {
            var             filenames    = new List <string> ();
            var             nativeDialog = (IFileOpenDialog)dialog.nativeDialog;
            IShellItemArray resultsArray;
            uint            count;

            var hr = nativeDialog.GetSelectedItems(out resultsArray);

            if (hr != 0)
            {
                var e = Marshal.GetExceptionForHR(hr);

                //we get E_FAIL when there is no selection
                if (hr == -2147467259)
                {
                    return(filenames);
                }
                else if (e is FileNotFoundException)
                {
                    return(filenames);
                }

                throw e;
            }

            hr = (int)resultsArray.GetCount(out count);
            if (hr != 0)
            {
                throw Marshal.GetExceptionForHR(hr);
            }

            for (int i = 0; i < count; ++i)
            {
                var    item = CommonFileDialog.GetShellItemAt(resultsArray, i);
                string val  = CommonFileDialog.GetFileNameFromShellItem(item);
                filenames.Add(val);
            }

            return(filenames);
        }