在 Windows 中用所有已注册的文件类型填充 ComboBox 最有效的方法是什么?
我想要完整的文件类型,而不仅仅是扩展名。我正在使用 VB 9 (VS2008)。
最佳答案
所有文件类型都存储在 HKEY_CLASS_ROOT 下的注册表中,您可以使用框架的 Registry class 获取它。 .
这是执行任务的 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;
}
}
关于c# - 用所有已注册的文件类型(不仅仅是扩展名)填充 ComboBox 的最有效方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/497777/