views:

148

answers:

3

What is the most efficient to fill a ComboBox with all the registered file types in Windows?

I want the full file type, not just the extension. I'm using VB 9 (VS2008).

Thank you.

A: 

I know this doesn't answer your question, but it's worth considering: on many systems, that's LOT of items. Perhaps a search or Listbox instead?

Joel Coehoorn
+4  A: 

All the file types are stored in the registry under the HKEY_CLASS_ROOT, which you could obtain using the Framework's Registry class.

Here's c# code to perform the task:

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;
    }
}
Mitch Wheat
+1  A: 

I agree with Joel, that's going to be a lot of entries and trying to find something in a combobox list of hundreds of items is going to end up as a really poor user experience. Other than that, the only way to get this information is to go through the registry, as Mitch says but it won't be simple code.

What are you trying to accomplish?

Edit: @Mitch Wheat, I know this was addressed to @Mark Brackett, but I couldn't resist the challenge. Using LINQ, your code can be written as:

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();
}
Scott Dorman
And this was downvoted why?
Scott Dorman
The code is simple (and has been posted now - looks to be about 20 lines, and it could be cut down at least 50% or so), you didn't answer the question, and your post essentially boils down to "me, too!". That said, I didn't downvote it, just offering critiques. ;)
Mark Brackett
@Mark Brackett: if you think you can cut that code I posted down by 50%, I suggest you post an answer showing how to do so!
Mitch Wheat
@Mitch Wheat: I know this was addressed to @Mark Brackett, but I couldn't resist the challenge. See the edit to my answer for a LINQ version.
Scott Dorman
@Scott Dorman: OK it's shorter, but is it easier to understand?
Mitch Wheat
@Mitch Wheat: That wasn't part of the original requirements. :) If you are familiar/comfortable with LINQ I would argue that it is as easy to understand as your code.
Scott Dorman