Esempio n. 1
0
        public FileSystemItemUI(DriveInfo drive)
        {
            FullName           = drive.Name;
            _cachedDisplayType = IOHelpers.GetDisplayType(drive.Name);
            FileSystemItemType = FileSystemItemType.Drive;
            IsDriveReady       = drive.IsReady;

            string name = " (" + drive.Name[0] + ":)";

            if (drive.IsReady)
            {
                try
                {
                    name        = (drive.VolumeLabel != "" ? drive.VolumeLabel : DisplayType) + name;
                    Size        = drive.TotalSize;
                    UsedSpace   = drive.TotalSize - drive.TotalFreeSpace;
                    FreeSpace   = drive.TotalFreeSpace;
                    DriveFormat = drive.DriveFormat;
                }
                catch
                {
                    name = drive.DriveType.ToString() + name;
                }
            }
            else
            {
                name = drive.DriveType.ToString() + name;
            }

            _cachedName = name;
        }
Esempio n. 2
0
        //public static readonly DependencyProperty LocationProperty = DependencyProperty.Register(
        //	"Location", typeof(string), typeof(Breadcrumb));

        //public string Location
        //{
        //	get { return (string)GetValue(LocationProperty); }
        //	set { SetValue(LocationProperty, value); }
        //}

        public async Task <string> Location()
        {
            if (Items.Count > 1)
            {
                string loc = await IOHelpers.Normalize(((BreadcrumbButton)Items[Items.Count - 1]).Location, false);

                if (loc == string.Empty)
                {
                    loc = ((BreadcrumbButton)Items[1]).Location.TrimEnd('\\');
                }

                return(loc);
            }
            else
            {
                return(null);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Gets a list of the applications registered to open a specified extension.
        /// </summary>
        /// <param name="extension">The extension to query, prefixed with a period.</param>
        /// <example>
        /// ExplorerContextMenuItem[] openWithList = await GetOpenWithList(".txt");
        /// </example>
        /// <returns></returns>
        public static async Task <ExplorerContextMenuItem[]> GetOpenWithList(string extension)
        {
            return(await Task.Factory.StartNew <ExplorerContextMenuItem[]>(() =>
            {
                try
                {
                    List <ExplorerContextMenuItem> items = new List <ExplorerContextMenuItem>();

                    // Keep a list of all the apps added - we don't want to add an app more than once.
                    HashSet <string> apps = new HashSet <string>();

                    //
                    // OpenWithList
                    //
                    RegistryKey openWithList = Registry.ClassesRoot.OpenSubKey(extension + "\\OpenWithList");

                    if (openWithList != null)
                    {
                        string[] openWith = openWithList.GetSubKeyNames();

                        foreach (string each in openWith)
                        {
                            ExplorerContextMenuItem?item = GetContextMenuItemApp(each);

                            if (item != null)
                            {
                                string appPath = IOHelpers.GetFileNameFromCommandLine(item.Value.Command);

                                if (!apps.Contains(appPath))
                                {
                                    apps.Add(appPath);
                                    items.Add(item.Value);
                                }
                            }
                        }
                    }

                    //
                    // OpenWithProgIds
                    //
                    RegistryKey progIds = Registry.ClassesRoot.OpenSubKey(extension + "\\OpenWithProgIds");

                    if (progIds != null)
                    {
                        string[] progIdsValues = progIds.GetValueNames();

                        foreach (string each in progIdsValues)
                        {
                            if (string.IsNullOrWhiteSpace(each))
                            {
                                continue;
                            }

                            ExplorerContextMenuItem?item = GetContextMenuItemProgId(each, "open");

                            if (item != null)
                            {
                                string appPath = IOHelpers.GetFileNameFromCommandLine(item.Value.Command);

                                if (!apps.Contains(appPath))
                                {
                                    apps.Add(appPath);
                                    items.Add(item.Value);
                                }
                            }
                        }
                    }

                    items.Sort(ExplorerContextMenuItemComparer);

                    return items.ToArray();
                }
                catch { }

                return null;
            }));
        }
Esempio n. 4
0
        private static ExplorerContextMenuItem?GetContextMenuItemApp(string app, string defaultDescription = null)
        {
            RegistryKey shell = Registry.ClassesRoot.OpenSubKey("Applications\\" + app + "\\shell");

            //RegistryKey appPaths = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\" + app);

            if (shell == null)
            {
                return(null);
            }

            RegistryKey open = shell.OpenSubKey("open");

            if (open == null)
            {
                open = shell.OpenSubKey(shell.GetSubKeyNames()[0]);
            }

            RegistryKey command = open.OpenSubKey("command");

            if (command == null)
            {
                return(null);
            }

            string appCommand = (string)command.GetValue(null);

            if (appCommand == null)
            {
                return(null);
            }

            string description;

            if (defaultDescription != null)
            {
                description = defaultDescription;
            }
            else
            {
                string filename = IOHelpers.GetFileNameFromCommandLine(appCommand);

                FileVersionInfo info = FileVersionInfo.GetVersionInfo(filename);

                description = info.FileDescription;

                if (string.IsNullOrEmpty(description))
                {
                    description = info.ProductName;
                }

                if (string.IsNullOrEmpty(description))
                {
                    description = Path.GetFileNameWithoutExtension(filename);
                }
            }

            return(new ExplorerContextMenuItem()
            {
                Command = appCommand, Header = description
            });
        }
Esempio n. 5
0
        private static ExplorerContextMenuItem?GetContextMenuItemProgId(string progId, string defaultAction = null, string defaultDescription = null)
        {
            if (defaultAction == null)
            {
                RegistryKey shell = Registry.ClassesRoot.OpenSubKey(progId + "\\Shell");

                if (shell == null)
                {
                    return(null);
                }

                defaultAction = (string)shell.GetValue(null, "open");
            }

            RegistryKey open = Registry.ClassesRoot.OpenSubKey(progId + "\\Shell\\" + defaultAction);

            if (open == null)
            {
                return(null);
            }

            RegistryKey handler = open.OpenSubKey("Command");

            if (handler == null)
            {
                return(null);
            }

            string description = (string)open.GetValue(null);
            string app         = (string)handler.GetValue(null);

            if (string.IsNullOrEmpty(description))
            {
                if (defaultDescription != null)
                {
                    description = defaultDescription;
                }
                else
                {
                    string          filename = IOHelpers.GetFileNameFromCommandLine(app);
                    FileVersionInfo info     = FileVersionInfo.GetVersionInfo(filename);

                    description = info.FileDescription;

                    if (string.IsNullOrEmpty(description))
                    {
                        description = info.ProductName;
                    }

                    if (string.IsNullOrEmpty(description))
                    {
                        description = Path.GetFileNameWithoutExtension(filename);
                    }
                }
            }
            else
            {
                description = description.Replace("_", "\\_").Replace('&', '_');
            }

            if (app != null)
            {
                return new ExplorerContextMenuItem()
                       {
                           Command = app, Header = description
                       }
            }
            ;

            return(null);
        }
Esempio n. 6
0
        //private static async Task<BitmapSource> GetCachedAsync(string uri, Func<Task<BitmapSource>> getIcon)
        //{
        //	BitmapSource data;

        //	if (_cache.TryGetValue(uri, out data))
        //		return data;

        //	data = await getIcon();

        //	try { _cache.Add(uri, data); }
        //	catch (ArgumentException)
        //	{
        //		// Since this method can be called asynchronously, there is a chance
        //		// that another call already created this key.
        //	}

        //	return data;
        //}

        public static async Task <BitmapSource> GetIcon(string fullName, IconSize size)
        {
            fullName = await IOHelpers.Normalize(fullName, false);

            return(await Task.Factory.StartNew <BitmapSource>(() =>
            {
                if (fullName == string.Empty)
                {
                    BitmapSource source = GetCached(fullName, size, GetDefaultComputerIcon);

                    if (source != null && source.CanFreeze)
                    {
                        source.Freeze();
                    }

                    return source;
                }
                else
                {
                    bool isFile = File.Exists(fullName);

                    string cacheKey = fullName;

                    string extension = null;

                    if (isFile)
                    {
                        extension = Path.GetExtension(fullName).ToLower();

                        // Icons, executables, links, and cursors have unique icons.
                        if (IsUniqueIcon(extension))
                        {
                            cacheKey = fullName;
                        }
                        else if (size == IconSize.ExtraSmall || !IsImage(extension))
                        {
                            cacheKey = extension;
                        }
                        else
                        {
                            cacheKey = fullName;
                        }
                    }

                    BitmapSource source = GetCached(cacheKey, size, () =>
                    {
                        BitmapSource img = !isFile || !IsImage(extension) ?
                                           IconExtractor.GetWin32Icon(fullName, size) : IconExtractor.GetImagePreview(fullName, extension, size);

                        if (img != null)
                        {
                            return img;
                        }

                        if (isFile)
                        {
                            return GetCached("<Fil>", size, GetDefaultFileIcon);
                        }
                        else
                        {
                            return GetCached("<Dir>", size, GetDefaultFolderIcon);
                        }
                    });

                    if (source != null && source.CanFreeze)
                    {
                        source.Freeze();
                    }

                    return source;
                }
            }));
        }