Exemplo n.º 1
0
        private void SetDialogProperties(FileDialogNative.IFileDialog dialog)
        {
            // Description
            if (!string.IsNullOrEmpty(descriptionText))
            {
                if (UseDescriptionForTitle)
                {
                    dialog.SetTitle(descriptionText);
                }
                else
                {
                    FileDialogNative.IFileDialogCustomize customize = (FileDialogNative.IFileDialogCustomize)dialog;
                    customize.AddText(0, descriptionText);
                }
            }

            dialog.SetOptions(FileDialogNative.FOS.FOS_PICKFOLDERS | FileDialogNative.FOS.FOS_FORCEFILESYSTEM | FileDialogNative.FOS.FOS_FILEMUSTEXIST);

            if (!string.IsNullOrEmpty(selectedPath))
            {
                string parent = Path.GetDirectoryName(selectedPath);
                if (parent == null || !Directory.Exists(parent))
                {
                    dialog.SetFileName(selectedPath);
                }
                else
                {
                    string folder = Path.GetFileName(selectedPath);
                    dialog.SetFolder(FileDialogNative.CreateItemFromParsingName(parent));
                    dialog.SetFileName(folder);
                }
            }
        }
Exemplo n.º 2
0
 private bool RunDialogVista(IntPtr owner)
 {
     FileDialogNative.IFileDialog dialog = null;
     try
     {
         dialog = new FileDialogNative.NativeFileOpenDialog();
         SetDialogProperties(dialog);
         int result = dialog.Show(owner);
         if (result < 0)
         {
             if ((uint)result == (uint)NativeMethods.HRESULT.ERROR_CANCELLED)
             {
                 return(false);
             }
             else
             {
                 throw Marshal.GetExceptionForHR(result);
             }
         }
         GetResult(dialog);
         return(true);
     }
     finally
     {
         if (dialog != null)
         {
             Marshal.FinalReleaseComObject(dialog);
         }
     }
 }
Exemplo n.º 3
0
 internal override string[] ProcessVistaFiles(FileDialogNative.IFileDialog dialog)
 {
     FileDialogNative.IFileOpenDialog openDialog = (FileDialogNative.IFileOpenDialog)dialog;
     if (Multiselect)
     {
         FileDialogNative.IShellItemArray results;
         openDialog.GetResults(out results);
         uint count;
         results.GetCount(out count);
         string[] files = new string[count];
         for (uint i = 0; i < count; ++i)
         {
             FileDialogNative.IShellItem item;
             results.GetItemAt(i, out item);
             files[unchecked ((int)i)] = GetFilePathFromShellItem(item);
         }
         return(files);
     }
     else
     {
         FileDialogNative.IShellItem item;
         openDialog.GetResult(out item);
         return(new string[] { GetFilePathFromShellItem(item) });
     }
 }
Exemplo n.º 4
0
 internal override string[] ProcessVistaFiles(FileDialogNative.IFileDialog dialog)
 {
     FileDialogNative.IFileSaveDialog saveDialog = (FileDialogNative.IFileSaveDialog)dialog;
     FileDialogNative.IShellItem      item;
     dialog.GetResult(out item);
     return(new string[] { GetFilePathFromShellItem(item) });
 }
Exemplo n.º 5
0
        internal virtual void OnBeforeVistaDialog(FileDialogNative.IFileDialog dialog)
        {
            dialog.SetDefaultExtension(this.DefaultExt);

            dialog.SetFileName(this.FileName);

            if (!string.IsNullOrEmpty(this.InitialDirectory))
            {
                try
                {
                    FileDialogNative.IShellItem initialDirectory = GetShellItemForPath(this.InitialDirectory);

                    dialog.SetDefaultFolder(initialDirectory);
                    dialog.SetFolder(initialDirectory);
                }
                catch (FileNotFoundException)
                {
                }
            }

            dialog.SetTitle(this.Title);

            dialog.SetOptions(GetOptions());

            SetFileTypes(dialog);

            this._customPlaces.Apply(dialog);
        }
        internal void Apply(FileDialogNative.IFileDialog dialog)
        {
            //Walk backwards
            for (int i = this.Items.Count - 1; i >= 0; --i)
            {
                FileDialogCustomPlace customPlace = this.Items[i];

                // Fix for Dev10 bug 536188: we need permission to check whether the specified path exists
                FileIOPermission permission = new FileIOPermission(FileIOPermissionAccess.PathDiscovery, customPlace.Path);
                permission.Demand();

                try
                {
                    FileDialogNative.IShellItem shellItem = customPlace.GetNativePath();
                    if (null != shellItem)
                    {
                        dialog.AddPlace(shellItem, 0);
                    }
                }
                catch (FileNotFoundException)
                {
                }
                //Silently absorb FileNotFound exceptions (these could be caused by a path that disappeared after the place was added to the dialog).
            }
        }
Exemplo n.º 7
0
 public int OnFileOk(FileDialogNative.IFileDialog pfd)
 {
     if (!this._dialog.HandleVistaFileOk(pfd))
     {
         return(1);
     }
     return(0);
 }
Exemplo n.º 8
0
 private void SetFileTypes(FileDialogNative.IFileDialog dialog)
 {
     FileDialogNative.COMDLG_FILTERSPEC[] filterItems = FilterItems;
     dialog.SetFileTypes((uint)filterItems.Length, filterItems);
     if (filterItems.Length > 0)
     {
         dialog.SetFileTypeIndex(unchecked ((uint)filterIndex));
     }
 }
Exemplo n.º 9
0
        private bool HandleVistaFileOk(FileDialogNative.IFileDialog dialog)
        {
            int options     = this.options;
            int filterIndex = this.filterIndex;

            string[] fileNames = this.fileNames;
            bool     securityCheckFileNames = this.securityCheckFileNames;
            bool     flag2 = false;

            try
            {
                uint num3;
                this.securityCheckFileNames = true;
                Thread.MemoryBarrier();
                dialog.GetFileTypeIndex(out num3);
                this.filterIndex = (int)num3;
                this.fileNames   = this.ProcessVistaFiles(dialog);
                if (!this.ProcessFileNames())
                {
                    return(flag2);
                }
                CancelEventArgs e = new CancelEventArgs();
                if (NativeWindow.WndProcShouldBeDebuggable)
                {
                    this.OnFileOk(e);
                    return(!e.Cancel);
                }
                try
                {
                    this.OnFileOk(e);
                    flag2 = !e.Cancel;
                }
                catch (Exception exception)
                {
                    Application.OnThreadException(exception);
                }
            }
            finally
            {
                if (!flag2)
                {
                    this.securityCheckFileNames = securityCheckFileNames;
                    Thread.MemoryBarrier();
                    this.fileNames   = fileNames;
                    this.options     = options;
                    this.filterIndex = filterIndex;
                }
                else if ((this.options & 4) != 0)
                {
                    this.options &= -2;
                }
            }
            return(flag2);
        }
 internal void Apply(FileDialogNative.IFileDialog dialog)
 {
     for (int i = base.Items.Count - 1; i >= 0; i--)
     {
         FileDialogCustomPlace place = base.Items[i];
         new FileIOPermission(FileIOPermissionAccess.PathDiscovery, place.Path).Demand();
         try
         {
             FileDialogNative.IShellItem nativePath = place.GetNativePath();
             if (nativePath != null)
             {
                 dialog.AddPlace(nativePath, 0);
             }
         }
         catch (FileNotFoundException)
         {
         }
     }
 }
Exemplo n.º 11
0
        private bool RunDialogVista(IntPtr hWndOwner)
        {
            FileDialogNative.IFileDialog dialog = CreateVistaDialog();
            OnBeforeVistaDialog(dialog);
            VistaDialogEvents events = new VistaDialogEvents(this);
            uint eventCookie;

            dialog.Advise(events, out eventCookie);
            try
            {
                int result = dialog.Show(hWndOwner);
                return(0 == result);
            }
            finally
            {
                dialog.Unadvise(eventCookie);
                //Make sure that the event interface doesn't get collected
                GC.KeepAlive(events);
            }
        }
        internal void Apply(FileDialogNative.IFileDialog dialog)
        {
            //Walk backwards
            for (int i = this.Items.Count - 1; i >= 0; --i)
            {
                FileDialogCustomPlace customPlace = this.Items[i];

                try
                {
                    FileDialogNative.IShellItem shellItem = customPlace.GetNativePath();
                    if (null != shellItem)
                    {
                        dialog.AddPlace(shellItem, 0);
                    }
                }
                catch (FileNotFoundException)
                {
                }
                //Silently absorb FileNotFound exceptions (these could be caused by a path that disappeared after the place was added to the dialog).
            }
        }
Exemplo n.º 13
0
        private bool RunDialogVista(IntPtr hWndOwner)
        {
            uint num;
            bool flag;

            FileDialogNative.IFileDialog dialog = this.CreateVistaDialog();
            this.OnBeforeVistaDialog(dialog);
            VistaDialogEvents pfde = new VistaDialogEvents(this);

            dialog.Advise(pfde, out num);
            try
            {
                int num2 = dialog.Show(hWndOwner);
                flag = 0 == num2;
            }
            finally
            {
                dialog.Unadvise(num);
                GC.KeepAlive(pfde);
            }
            return(flag);
        }
Exemplo n.º 14
0
 internal override string[] ProcessVistaFiles(FileDialogNative.IFileDialog dialog)
 {
     FileDialogNative.IShellItem      item2;
     FileDialogNative.IFileOpenDialog dialog2 = (FileDialogNative.IFileOpenDialog)dialog;
     if (this.Multiselect)
     {
         FileDialogNative.IShellItemArray array;
         uint num;
         dialog2.GetResults(out array);
         array.GetCount(out num);
         string[] strArray = new string[num];
         for (uint i = 0; i < num; i++)
         {
             FileDialogNative.IShellItem item;
             array.GetItemAt(i, out item);
             strArray[i] = FileDialog.GetFilePathFromShellItem(item);
         }
         return(strArray);
     }
     dialog2.GetResult(out item2);
     return(new string[] { FileDialog.GetFilePathFromShellItem(item2) });
 }
Exemplo n.º 15
0
 public int OnFolderChanging(FileDialogNative.IFileDialog pfd, FileDialogNative.IShellItem psiFolder)
 {
     return(0);
 }
Exemplo n.º 16
0
 internal abstract string[] ProcessVistaFiles(FileDialogNative.IFileDialog dialog);
Exemplo n.º 17
0
 public void OnTypeChange(FileDialogNative.IFileDialog pfd)
 {
 }
Exemplo n.º 18
0
 public void OnOverwrite(FileDialogNative.IFileDialog pfd, FileDialogNative.IShellItem psi, out FileDialogNative.FDE_OVERWRITE_RESPONSE pResponse)
 {
     pResponse = FileDialogNative.FDE_OVERWRITE_RESPONSE.FDEOR_DEFAULT;
 }
Exemplo n.º 19
0
 public void OnSelectionChange(FileDialogNative.IFileDialog pfd)
 {
 }
Exemplo n.º 20
0
 public void OnShareViolation(FileDialogNative.IFileDialog pfd, FileDialogNative.IShellItem psi, out FileDialogNative.FDE_SHAREVIOLATION_RESPONSE pResponse)
 {
     pResponse = FileDialogNative.FDE_SHAREVIOLATION_RESPONSE.FDESVR_DEFAULT;
 }
Exemplo n.º 21
0
 public void OnFolderChange(FileDialogNative.IFileDialog pfd)
 {
 }
Exemplo n.º 22
0
 public int OnFolderChanging(FileDialogNative.IFileDialog pfd, FileDialogNative.IShellItem psiFolder)
 {
     return(NativeMethods.S_OK);
 }
Exemplo n.º 23
0
 public int OnFileOk(FileDialogNative.IFileDialog pfd)
 {
     return(this._dialog.HandleVistaFileOk(pfd) ? NativeMethods.S_OK : NativeMethods.S_FALSE);
 }
Exemplo n.º 24
0
        internal System.Windows.Forms.DialogResult ShowDialog(System.Windows.Forms.IWin32Window owner, FileDialogNative.IFileDialog f)
        {
            if (!string.IsNullOrEmpty(this.InitialDirectory))
            {
                FileDialogNative.IShellItem item = null;
                try
                {
                    SHCreateItemFromParsingName(this.InitialDirectory, IntPtr.Zero, typeof(FileDialogNative.IShellItem).GUID, out item);
                    f.SetFolder(item);
                }
                catch (Exception e)
                {
                    Util.Logging.Log(e);
                }
                if (item != null)
                {
                    Marshal.ReleaseComObject(item);
                }
            }

            if (!string.IsNullOrEmpty(this.FileName))
            {
                f.SetFileName(this.FileName);
            }

            if (!string.IsNullOrEmpty(this.Filter))
            {
                var filter  = this.Filter.Split('|');
                var filters = new FileDialogNative.COMDLG_FILTERSPEC[filter.Length / 2];

                for (var i = 0; i < filter.Length; i += 2)
                {
                    filters[i / 2] = new FileDialogNative.COMDLG_FILTERSPEC()
                    {
                        pszName = filter[i],
                        pszSpec = filter[i + 1],
                    };
                }

                f.SetFileTypes((uint)filters.Length, filters);
                f.SetFileTypeIndex((uint)this.FilterIndex);
            }

            if (!string.IsNullOrEmpty(this.Title))
            {
                f.SetTitle(this.Title);
            }

            if (f.Show(owner.Handle) == 0)
            {
                FileDialogNative.IShellItem item;
                f.GetResult(out item);

                string ppszName;
                item.GetDisplayName(FileDialogNative.SIGDN.SIGDN_FILESYSPATH, out ppszName);
                Marshal.ReleaseComObject(item);

                this.FileName = ppszName;

                return(System.Windows.Forms.DialogResult.OK);
            }

            return(System.Windows.Forms.DialogResult.Cancel);
        }
Exemplo n.º 25
0
 private protected override string[] ProcessVistaFiles(FileDialogNative.IFileDialog dialog) => null;
Exemplo n.º 26
0
        private bool HandleVistaFileOk(FileDialogNative.IFileDialog dialog)
        {
            int saveOptions     = options;
            int saveFilterIndex = filterIndex;

            string[] saveFileNames = fileNames;
            bool     ok            = false;

            try
            {
                Thread.MemoryBarrier();
                uint filterIndexTemp;
                dialog.GetFileTypeIndex(out filterIndexTemp);
                filterIndex = unchecked ((int)filterIndexTemp);
                fileNames   = ProcessVistaFiles(dialog);
                if (ProcessFileNames())
                {
                    CancelEventArgs ceevent = new CancelEventArgs();
                    if (NativeWindow.WndProcShouldBeDebuggable)
                    {
                        OnFileOk(ceevent);
                        ok = !ceevent.Cancel;
                    }
                    else
                    {
                        try
                        {
                            OnFileOk(ceevent);
                            ok = !ceevent.Cancel;
                        }
                        catch (Exception e)
                        {
                            Application.OnThreadException(e);
                        }
                    }
                }
            }
            finally
            {
                if (!ok)
                {
                    Thread.MemoryBarrier();
                    fileNames = saveFileNames;

                    options     = saveOptions;
                    filterIndex = saveFilterIndex;
                }
                else
                {
                    if (0 != (options & NativeMethods.OFN_HIDEREADONLY))
                    {
                        //When the dialog is dismissed OK, the Readonly bit can't
                        // be left on if ShowReadOnly was false
                        // Downlevel this happens automatically, on Vista mode, we need to watch out for it.

                        options &= ~NativeMethods.OFN_READONLY;
                    }
                }
            }
            return(ok);
        }
Exemplo n.º 27
0
 private void GetResult(FileDialogNative.IFileDialog dialog)
 {
     dialog.GetResult(out FileDialogNative.IShellItem item);
     item.GetDisplayName(FileDialogNative.SIGDN.SIGDN_FILESYSPATH, out selectedPath);
 }