コード例 #1
0
        int NativeInterfaces.IFileDialogEvents.OnFileOk(NativeInterfaces.IFileDialog pfd)
        {
            int hr = NativeConstants.S_OK;

            NativeInterfaces.IShellItem shellItem = null;

            if (NativeMethods.SUCCEEDED(hr))
            {
                hr = FileSaveDialog.GetResult(out shellItem);
            }

            if (!NativeMethods.SUCCEEDED(hr))
            {
                throw Marshal.GetExceptionForHR(hr);
            }

            string pathName = null;

            try
            {
                shellItem.GetDisplayName(NativeConstants.SIGDN.SIGDN_FILESYSPATH, out pathName);
            }

            finally
            {
                if (shellItem != null)
                {
                    try
                    {
                        Marshal.ReleaseComObject(shellItem);
                    }

                    catch (Exception)
                    {
                    }

                    shellItem = null;
                }
            }

            string pathNameResolved = ResolveName(pathName);
            NativeInterfaces.IOleWindow oleWindow = (NativeInterfaces.IOleWindow)pfd;

            try
            {
                IntPtr hWnd = IntPtr.Zero;
                oleWindow.GetWindow(out hWnd);
                Win32Window win32Window = new Win32Window(hWnd, oleWindow);

                // File name/path validation
                if (hr >= 0)
                {
                    try
                    {
                        // Verify that these can be parsed correctly
                        string fileName = Path.GetFileName(pathNameResolved);
                        string dirName = Path.GetDirectoryName(pathNameResolved);
                    }

                    catch (Exception ex)
                    {
                        if (!FileDialogUICallbacks.ShowError(win32Window, pathNameResolved, ex))
                        {
                            throw;
                        }

                        hr = NativeConstants.S_FALSE;
                    }
                }

                if (hr >= 0)
                {
                    // Overwrite existing file
                    if (!OverwritePrompt)
                    {
                        hr = NativeConstants.S_OK;
                    }
                    else if (File.Exists(pathNameResolved))
                    {
                        FileOverwriteAction action = FileDialogUICallbacks.ShowOverwritePrompt(win32Window, pathNameResolved);

                        switch (action)
                        {
                            case FileOverwriteAction.Cancel:
                                hr = NativeConstants.S_FALSE;
                                break;

                            case FileOverwriteAction.Overwrite:
                                hr = NativeConstants.S_OK;
                                break;

                            default:
                                throw new InvalidEnumArgumentException();
                        }
                    }
                }
            }

            catch (Exception)
            {
            }

            finally
            {
                try
                {
                    Marshal.ReleaseComObject(oleWindow);
                }

                catch (Exception)
                {
                }

                oleWindow = null;
            }

            return hr;
        }
コード例 #2
0
        int NativeInterfaces.IFileDialogEvents.OnFileOk(NativeInterfaces.IFileDialog pfd)
        {
            int hr = NativeConstants.S_OK;

            NativeInterfaces.IShellItemArray results = null;
            FileOpenDialog.GetResults(out results);

            uint count = 0;
            results.GetCount(out count);

            List<NativeInterfaces.IShellItem> items = new List<NativeInterfaces.IShellItem>();
            List<NativeInterfaces.IShellItem> needLocalCopy = new List<NativeInterfaces.IShellItem>();
            List<NativeInterfaces.IShellItem> cannotCopy = new List<NativeInterfaces.IShellItem>();
            List<string> localPathNames = new List<string>();

            for (uint i = 0; i < count; ++i)
            {
                NativeInterfaces.IShellItem item = null;
                results.GetItemAt(i, out item);
                items.Add(item);
            }

            foreach (NativeInterfaces.IShellItem item in items)
            {
                // If it's a file system object, nothing special needs to be done.
                NativeConstants.SFGAO sfgaoAttribs;
                item.GetAttributes((NativeConstants.SFGAO)0xffffffff, out sfgaoAttribs);

                if ((sfgaoAttribs & NativeConstants.SFGAO.SFGAO_FILESYSTEM) == NativeConstants.SFGAO.SFGAO_FILESYSTEM)
                {
                    string pathName = null;
                    item.GetDisplayName(NativeConstants.SIGDN.SIGDN_FILESYSPATH, out pathName);

                    localPathNames.Add(pathName);
                }
                else if ((sfgaoAttribs & NativeConstants.SFGAO.SFGAO_STREAM) == NativeConstants.SFGAO.SFGAO_STREAM)
                {
                    needLocalCopy.Add(item);
                }
                else
                {
                    cannotCopy.Add(item);
                }
            }

            Marshal.ReleaseComObject(results);
            results = null;

            if (needLocalCopy.Count > 0)
            {
                IntPtr hwnd = IntPtr.Zero;
                NativeInterfaces.IOleWindow oleWindow = (NativeInterfaces.IOleWindow)pfd;
                oleWindow.GetWindow(out hwnd);
                Win32Window win32Window = new Win32Window(hwnd, oleWindow);

                IFileTransferProgressEvents progressEvents = this.FileDialogUICallbacks.CreateFileTransferProgressEvents();

                ThreadStart copyThreadProc =
                    delegate()
                    {
                        try
                        {
                            progressEvents.SetItemCount(needLocalCopy.Count);

                            for (int i = 0; i < needLocalCopy.Count; ++i)
                            {
                                NativeInterfaces.IShellItem item = needLocalCopy[i];

                                string pathName = null;

                                progressEvents.SetItemOrdinal(i);
                                CopyResult result = CreateLocalCopy(item, progressEvents, out pathName);

                                if (result == CopyResult.Success)
                                {
                                    localPathNames.Add(pathName);
                                }
                                else if (result == CopyResult.Skipped)
                                {
                                    // do nothing
                                }
                                else if (result == CopyResult.CancelOperation)
                                {
                                    hr = NativeConstants.S_FALSE;
                                    break;
                                }
                                else
                                {
                                    throw new InvalidEnumArgumentException();
                                }
                            }
                        }

                        finally
                        {
                            OperationResult result;

                            if (hr == NativeConstants.S_OK)
                            {
                                result = OperationResult.Finished;
                            }
                            else
                            {
                                result = OperationResult.Canceled;
                            }

                            progressEvents.EndOperation(result);
                        }
                    };

                Thread copyThread = new Thread(copyThreadProc);
                copyThread.SetApartmentState(ApartmentState.STA);

                EventHandler onUIShown =
                    delegate(object sender, EventArgs e)
                    {
                        copyThread.Start();
                    };

                this.cancelSink = new CancelableTearOff();
                progressEvents.BeginOperation(win32Window, onUIShown, cancelSink);
                this.cancelSink = null;
                copyThread.Join();

                Marshal.ReleaseComObject(oleWindow);
                oleWindow = null;
            }

            this.FileNames = localPathNames.ToArray();

            // If they selected a bunch of files, and then they all errored or something, then don't proceed.
            if (this.FileNames.Length == 0)
            {
                hr = NativeConstants.S_FALSE;
            }

            foreach (NativeInterfaces.IShellItem item in items)
            {
                Marshal.ReleaseComObject(item);
            }

            items.Clear();
            items = null;

            GC.KeepAlive(pfd);
            return hr;
        }