1

在 Windows 中用所有已注册的文件类型填充 ComboBox 最有效的是什么?

我想要完整的文件类型,而不仅仅是扩展名。我正在使用 VB 9 (VS2008)。

4

4 回答 4

4

所有文件类型都存储在 HKEY_CLASS_ROOT 下的注册表中,您可以使用框架的Registry 类来获取它。

这是执行任务的 c# 代码:

using Microsoft.Win32;

public class FileAssoc
{
    public string Extension;
    public string Filetype;

    public FileAssoc(string fileext, string name)
    {
        Extension = fileext;
        Filetype = name;
    }
}

public static class EnumRegFiles
{
    public static List<FileAssoc> GetFileAssociations()
    {
        List<FileAssoc> result = new List<FileAssoc>();
        RegistryKey rk = Registry.ClassesRoot;

        String[] names = rk.GetSubKeyNames();
        foreach (string file in names)
        {
            if (file.StartsWith("."))
            {
                RegistryKey rkey = rk.OpenSubKey(file);
                object descKey = rkey.GetValue("");

                if (descKey != null)
                {
                    string desc = descKey.ToString();
                    if (!string.IsNullOrEmpty(desc))
                    {
                        result.Add(new FileAssoc(file, desc));
                    }
                }
            }
        }

        return result;
    }
}
于 2009-01-31T00:23:13.403 回答
1

我同意 Joel 的观点,这将是很多条目,并且试图在包含数百个项目的组合框列表中查找某些内容最终会导致用户体验非常糟糕。除此之外,获取此信息的唯一方法是通过注册表,正如 Mitch 所说,但这不是简单的代码。

你想达到什么目的?

编辑: @Mitch Wheat,我知道这是写给@Mark Brackett,但我无法抗拒挑战。使用 LINQ,您的代码可以写成:

public static IList GetFileAssociations()
{
    return Registry.ClassesRoot.GetSubKeyNames().Where(key => key.StartsWith(".")).Select(key =>
    {
        string description = Registry.ClassesRoot.OpenSubKey(key).GetValue("") as string;
        if (!String.IsNullOrEmpty(description))
        {
            return new { key, description };
        }
        else
        {
            return null;
        }
    }).Where(a => a != null).ToList();
}
于 2009-01-31T00:46:28.850 回答
1
using Microsoft.Win32;
using System.Collections;
internal static class Extensions
{
    /// <summary>
    /// Gets a dictionary containing known file extensions and description from HKEY_CLASSES_ROOT.
    /// </summary>
    /// <returns>dictionary containing extensions and description.</returns>
    public static Dictionary<string, string> GetAllRegisteredFileExtensions()
    {
        //get into the HKEY_CLASSES_ROOT
        RegistryKey root = Registry.ClassesRoot;

        //generic list to hold all the subkey names
        Dictionary<string, string> subKeys = new Dictionary<string, string>();

        //IEnumerator for enumerating through the subkeys
        IEnumerator enums = root.GetSubKeyNames().GetEnumerator();

        //make sure we still have values
        while (enums.MoveNext())
        {
            //all registered extensions start with a period (.) so
            //we need to check for that
            if (enums.Current.ToString().StartsWith("."))
                //valid extension so add it and the default description if it exists
                subKeys.Add(enums.Current.ToString(), Registry.GetValue(root.Name + "\\" + enums.Current.ToString(), "", "").ToString());
        }
        return subKeys;
    }   
}
于 2012-10-04T16:29:08.200 回答
0

我知道这不能回答您的问题,但值得考虑:在许多系统上,有很多项目。也许是搜索或列表框?

于 2009-01-31T00:15:28.910 回答