Exemplo n.º 1
0
 private void Init(Uri uri)
 {
     if (uri.Scheme == "file")
     {
         Init(ShellUtil.GetShellItemForPath(uri.LocalPath));
     }
     else if (uri.Scheme == "shell")
     {
         var path           = uri.GetComponents(UriComponents.Path, UriFormat.Unescaped);
         var knownFolder    = path;
         var restOfPath     = string.Empty;
         var separatorIndex = path.IndexOf('/');
         if (separatorIndex != -1)
         {
             knownFolder = path.Substring(0, separatorIndex);
             restOfPath  = path.Substring(separatorIndex + 1);
         }
         if (restOfPath != string.Empty)
         {
             Init(this[restOfPath.Replace('/', '\\')].iShellItem);
         }
         else
         {
             Init(ShellUtil.GetKnownFolderFromPath(knownFolder));
         }
     }
     else
     {
         throw new InvalidOperationException("Invalid scheme.");
     }
 }
Exemplo n.º 2
0
        /// <summary>Initializes a new instance of the <see cref="ShellItem"/> class.</summary>
        /// <param name="parent">The parent Shell item.</param>
        /// <param name="name">The name of the child item.</param>
        public ShellItem(ShellItem parent, string name)
        {
            if (parent == null)
            {
                throw new ArgumentNullException(nameof(parent));
            }
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (!parent.IsFolder)
            {
                throw new ArgumentException("Parent argument must be a folder.");
            }

            if (parent.IsFileSystem)
            {
                Init(ShellUtil.GetShellItemForPath(Path.Combine(parent.FileSystemPath, name)));
            }
            else
            {
                var   eaten = 0;
                PIDL  tempPidl;
                SFGAO attr = 0;
                parent.GetIShellFolder().ParseDisplayName(IntPtr.Zero, null, name, ref eaten, out tempPidl, ref attr);
                Init(tempPidl);
            }
        }
Exemplo n.º 3
0
        public static IShellItem CreateShellItem(string path)
        {
            if (!PathFileExists(path))
            {
                return(null);
            }

            return(ShellUtil.GetShellItemForPath(path));
        }
 private static IShellItem2 CreateItemFromJumpPath(JumpPath jumpPath)
 {
     try
     {
         return(ShellUtil.GetShellItemForPath(Path.GetFullPath(jumpPath.Path)));
     }
     catch (Exception)
     {
     }
     return(null);
 }
Exemplo n.º 5
0
        private static bool InitializeWithItem(string pszFile, IPreviewHandler previewHandler)
        {
            if (previewHandler is IInitializeWithItem initializeWithItem)
            {
                var psi = ShellUtil.GetShellItemForPath(pszFile);
                initializeWithItem.Initialize(psi, STGM.STGM_READ).ThrowIfFailed("InitializeWithItem::Initialize");
                Log.Debug("PreviewHandler::InitializeWithItem");
                return(true);
            }

            return(false);
        }
Exemplo n.º 6
0
        private static IShellItem2 CreateItemFromJumpPath(JumpPath jumpPath)
        {
            Debug.Assert(jumpPath != null);

            try
            {
                // This will return null if the path doesn't exist.
                return(ShellUtil.GetShellItemForPath(Path.GetFullPath(jumpPath.Path)));
            }
            catch (Exception)
            {
                // Don't propagate exceptions here.  If we couldn't create the item, it's just invalid.
            }

            return(null);
        }
Exemplo n.º 7
0
 /// <summary>Initializes a new instance of the <see cref="ShellItem"/> class.</summary>
 /// <param name="knownFolder">A known folder reference.</param>
 protected ShellItem(KNOWNFOLDERID knownFolder)
 {
     if (IsMin7)
     {
         SHGetKnownFolderItem(knownFolder.Guid(), KNOWN_FOLDER_FLAG.KF_FLAG_DEFAULT, SafeTokenHandle.Null, typeof(IShellItem).GUID, out var ppv).ThrowIfFailed();
         Init((IShellItem)ppv);
     }
     else
     {
         var csidl = knownFolder.SpecialFolder();
         if (csidl == null)
         {
             throw new ArgumentOutOfRangeException(nameof(knownFolder), @"Cannot translate this known folder to a value understood by systems prior to Windows 7.");
         }
         var path = new StringBuilder(MAX_PATH);
         SHGetFolderPath(IntPtr.Zero, (int)csidl.Value, SafeTokenHandle.Null, SHGFP.SHGFP_TYPE_CURRENT, path).ThrowIfFailed();
         Init(ShellUtil.GetShellItemForPath(path.ToString()));
     }
 }
Exemplo n.º 8
0
        internal virtual void PrepareVistaDialog(IFileDialog dialog)
        {
            dialog.SetDefaultExtension(this.DefaultExt);
            dialog.SetFileName(this.CriticalFileName);
            if (!string.IsNullOrEmpty(this.InitialDirectory))
            {
                IShellItem shellItemForPath = ShellUtil.GetShellItemForPath(this.InitialDirectory);
                if (shellItemForPath != null)
                {
                    dialog.SetDefaultFolder(shellItemForPath);
                    dialog.SetFolder(shellItemForPath);
                }
            }
            dialog.SetTitle(this.Title);
            FOS options = (FOS)((this.Options & 1063690) | 536870912 | 64);

            dialog.SetOptions(options);
            COMDLG_FILTERSPEC[] filterItems = FileDialog.GetFilterItems(this.Filter);
            if (filterItems.Length != 0)
            {
                dialog.SetFileTypes((uint)filterItems.Length, filterItems);
                dialog.SetFileTypeIndex((uint)this.FilterIndex);
            }
            IList <FileDialogCustomPlace> customPlaces = this.CustomPlaces;

            if (customPlaces != null && customPlaces.Count != 0)
            {
                foreach (FileDialogCustomPlace customPlace in customPlaces)
                {
                    IShellItem shellItem = FileDialog.ResolveCustomPlace(customPlace);
                    if (shellItem != null)
                    {
                        try
                        {
                            dialog.AddPlace(shellItem, FDAP.BOTTOM);
                        }
                        catch (ArgumentException)
                        {
                        }
                    }
                }
            }
        }
Exemplo n.º 9
0
 /// <summary>Initializes a new instance of the <see cref="ShellItem"/> class.</summary>
 /// <param name="path">The file system path of the item.</param>
 public ShellItem(string path)
 {
     Init(ShellUtil.GetShellItemForPath(path));
 }
Exemplo n.º 10
0
        protected override void PerformAction(object parameter)
        {
            var photo = parameter as FacebookPhoto;

            if (photo == null)
            {
                return;
            }

            string defaultFileName = "Facebook Photo";

            if (photo.Album != null)
            {
                defaultFileName = photo.Album.Title + " (" + (photo.Album.Photos.IndexOf(photo) + 1) + ")";
            }

            string filePath = null;

            if (Utility.IsOSVistaOrNewer)
            {
                IFileSaveDialog pFileSaveDialog = null;
                try
                {
                    pFileSaveDialog = CLSID.CoCreateInstance <IFileSaveDialog>(CLSID.FileSaveDialog);
                    pFileSaveDialog.SetOptions(pFileSaveDialog.GetOptions() | FOS.FORCEFILESYSTEM | FOS.OVERWRITEPROMPT);
                    pFileSaveDialog.SetTitle("Select where to save the photo");
                    pFileSaveDialog.SetOkButtonLabel("Save Photo");
                    var filterspec = new COMDLG_FILTERSPEC {
                        pszName = "Images", pszSpec = "*.jpg;*.png;*.bmp;*.gif"
                    };
                    pFileSaveDialog.SetFileTypes(1, ref filterspec);
                    pFileSaveDialog.SetFileName(defaultFileName);
                    Guid clientId = _SavePhotoId;
                    pFileSaveDialog.SetClientGuid(ref clientId);

                    IShellItem pItem = null;
                    try
                    {
                        pItem = ShellUtil.GetShellItemForPath(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));
                        pFileSaveDialog.SetDefaultFolder(pItem);
                    }
                    finally
                    {
                        Utility.SafeRelease(ref pItem);
                    }

                    HRESULT hr = pFileSaveDialog.Show(new WindowInteropHelper(Application.Current.MainWindow).Handle);
                    if (hr.Failed)
                    {
                        Assert.AreEqual((HRESULT)Win32Error.ERROR_CANCELLED, hr);
                        return;
                    }

                    pItem = null;
                    try
                    {
                        pItem    = pFileSaveDialog.GetResult();
                        filePath = ShellUtil.GetPathFromShellItem(pItem);
                    }
                    finally
                    {
                        Utility.SafeRelease(ref pItem);
                    }
                }
                finally
                {
                    Utility.SafeRelease(ref pFileSaveDialog);
                }
            }
            else
            {
                var saveFileDialog = new Microsoft.Win32.SaveFileDialog
                {
                    Filter           = "Image Files|*.jpg;*.png;*.bmp;*.gif",
                    FileName         = defaultFileName,
                    InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures),
                };

                if (saveFileDialog.ShowDialog(Application.Current.MainWindow) != true)
                {
                    return;
                }

                filePath = saveFileDialog.FileName;
            }

            FacebookImageSaveOptions fiso = FacebookImageSaveOptions.FindBetterName;

            // We told the file dialog to prompt about overwriting, so if the user specified a location
            // with a file extension and the file already exists, prepare to overwrite.
            // This isn't quite right because the file extension may be different, so we may overwrite a jpg
            // when it was asked to be a gif, but it's not a likely scenario.
            if (System.IO.File.Exists(filePath))
            {
                fiso = FacebookImageSaveOptions.Overwrite;
            }

            photo.Image.SaveToFile(FacebookImageDimensions.Big, filePath, true, fiso, _OnPhotoSaveProgressCallback, null);
        }
Exemplo n.º 11
0
 private static IShellItem ResolveCustomPlace(FileDialogCustomPlace customPlace)
 {
     return(ShellUtil.GetShellItemForPath(ShellUtil.GetPathForKnownFolder(customPlace.KnownFolder) ?? customPlace.Path));
 }