예제 #1
0
        public static PackedDirectoryEntry GetDirectoryEntry(DirectoryInfoEx directory, string parentFolder)
        {
            if (!directory.Exists)
            {
                try
                {
                    var guid = new Guid(directory.FullName);
                    directory = new DirectoryInfoEx(KnownFolder.FromKnownFolderId(guid));
                    if (!directory.Exists)
                    {
                        return(null);
                    }
                }
                catch (Exception)
                {
                    return(null);
                }
            }

            PackedDirectoryEntry result;

            if (directory.DirectoryType == DirectoryInfoEx.DirectoryTypeEnum.dtDrive)
            {
                var drive = DriveInfo.GetDrives().FirstOrDefault(x => x.RootDirectory.FullName == directory.FullName);
                if (drive != null)
                {
                    if (!drive.IsReady)
                    {
                        result = new DriveDirectoryEntry
                        {
                            TotalSize = 0,
                            UsedSpace = 0,
                            DriveType = (DriveDirectoryType)drive.DriveType
                        }
                    }
                }
                ;
                else
                {
                    result = new DriveDirectoryEntry
                    {
                        TotalSize = drive.TotalSize,
                        UsedSpace = drive.TotalSize - drive.TotalFreeSpace,
                        DriveType = (DriveDirectoryType)drive.DriveType
                    }
                };
예제 #2
0
        //private static readonly Lazy<Dictionary<Environment.SpecialFolder, ShellAPI.CSIDL>> _shellFolderLookupDic =
        //    new Lazy<Dictionary<Environment.SpecialFolder, ShellAPI.CSIDL>>(() => constructShellFolderLookupDic());
        //private static readonly Lazy<Dictionary<Environment.SpecialFolder, string>> _directoryFullNameLookupDic =
        //    new Lazy<Dictionary<Environment.SpecialFolder, string>>(() => constructDirectoryFullNameLookupDic());
        //private static readonly Lazy<Dictionary<string, KnownFolder>> _pathToKnownFolderLookupDic =
        //    new Lazy<Dictionary<string, KnownFolder>>(() => constructpathToKnownFolderLookupDic());

        private static Dictionary <string, KnownFolder> constructpathToKnownFolderLookupDic()
        {
            Dictionary <string, KnownFolder> dic = new Dictionary <string, KnownFolder>();

            foreach (var kfId in Enum.GetValues(typeof(KnownFolderIds)))
            {
                var category = EnumAttributeUtils <FolderCategoryAttribute, KnownFolderIds> .FindAttribute(kfId);

                if (category == null)
                {
                    Debug.WriteLine(String.Format("Attribute {0} does not have FolderCategoryAttribute", kfId));
                }
                else
                {
                    if (category.Category != KnownFolderCategory.Virtual)
                    {
                        KnownFolder kf   = KnownFolder.FromKnownFolderId((KnownFolderIds)kfId);
                        string      path = kf.Path;
                        if (!dic.ContainsKey(path))
                        {
                            dic.Add(path, kf);
                        }
                    }
                }
            }

            //foreach (var kf in KnownFolder.GetKnownFolders())
            //    if (kf.Category != KnownFolderCategory.Virtual)
            //    {
            //        string path = kf.Path;
            //        if (path != null)
            //        {
            //            if (!dic.ContainsKey(path))
            //                dic.Add(path, kf);
            //        }
            //    }

            return(dic);
        }
예제 #3
0
 public DirectoryInfoEx(KnownFolderIds knownFolderId)
     : this(KnownFolder.FromKnownFolderId(
                EnumAttributeUtils <KnownFolderGuidAttribute, KnownFolderIds> .FindAttribute(knownFolderId).Guid))
 {
 }
예제 #4
0
        public List <DirectoryEntry> GetNamespaceDirectories()
        {
            var result = new Dictionary <DirectoryEntry, int>();

            try
            {
                using (var rootKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Classes\CLSID"))
                {
                    if (rootKey != null)
                    {
                        foreach (var subKeyName in rootKey.GetSubKeyNames())
                        {
                            using (var possibleEntryRegKey = rootKey.OpenSubKey(subKeyName))
                            {
                                if ((int?)possibleEntryRegKey?.GetValue("System.IsPinnedToNameSpaceTree", 0) != 1)
                                {
                                    continue;
                                }

                                using (var infoKey = possibleEntryRegKey.OpenSubKey("Instance\\InitPropertyBag"))
                                {
                                    DirectoryEntry entry;
                                    var            folderPath = (string)infoKey?.GetValue("TargetFolderPath", null);
                                    if (folderPath != null)
                                    {
                                        using (var directory = new DirectoryInfoEx(folderPath))
                                            entry = GetDirectoryEntry(directory, null);
                                    }
                                    else if ((folderPath = (string)infoKey?.GetValue("TargetKnownFolder")) != null && Guid.TryParse(folderPath, out var folderId))
                                    {
                                        try
                                        {
                                            using (var directory = new DirectoryInfoEx(KnownFolder.FromKnownFolderId(folderId)))
                                                entry = GetDirectoryEntry(directory, null);
                                        }
                                        catch (Exception)
                                        {
                                            continue;
                                        }
                                    }
                                    else
                                    {
                                        continue;
                                    }

                                    if (entry == null)
                                    {
                                        continue;
                                    }

                                    var label = (string)possibleEntryRegKey.GetValue("");
                                    if (!string.IsNullOrEmpty(label))
                                    {
                                        entry.Label = label;
                                    }

                                    result.Add(entry,
                                               (int?)possibleEntryRegKey.GetValue("SortOrderIndex", null) ??
                                               int.MaxValue - 1);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                // Requested registry access is not allowed
                Log.Logger.Warning(e, "Error when accessing registry at SOFTWARE\\Classes\\CLSID");
            }

            result.Add(GetDirectoryEntry(DirectoryInfoEx.RecycleBinDirectory, null), -1);
            return(result.OrderBy(x => x.Value).Select(x => x.Key).ToList());
        }