创建两个表单并使用如下代码:
主窗体内部:
public void SelectProgram(string ext)
{
IEnumerable<string> programList = RecommendedPrograms(ext);
if (programList.Count() > 0)
{
// open a new form to show the program in the list (to user select one of them)
frmSelectProgram frmSP = new frmSelectProgram(programList);
frmSP.ShowDialog();
}
else
{
// show an Open Dialog to the user to select a program
}
}
并使用如下方法查找与女巫程序关联的文件(扩展名):(此方法由@LarsTech 编写,我更改了其中的一些行。)
using Microsoft.Win32;
public IEnumerable<string> RecommendedPrograms(string ext)
{
List<string> progs = new List<string>();
string baseKey = @"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\." + ext;
using (RegistryKey rk = Registry.CurrentUser.OpenSubKey(baseKey + @"\OpenWithList"))
{
if (rk != null)
{
string mruList = (string)rk.GetValue("MRUList");
if (mruList != null)
{
foreach (char c in mruList.ToString())
if(rk.GetValue(c.ToString())!=null)
progs.Add(rk.GetValue(c.ToString()).ToString());
}
}
}
using (RegistryKey rk = Registry.CurrentUser.OpenSubKey(baseKey + @"\OpenWithProgids"))
{
if (rk != null)
{
foreach (string item in rk.GetValueNames())
progs.Add(item);
}
//TO DO: Convert ProgID to ProgramName, etc.
}
return progs;
}
在 frmSelectProgram 表单中:
public partial class frmSelectProgram : Form
{
private IEnumerable<string> _programList;
public frmSelectProgram(IEnumerable<string> programList)
{
InitializeComponent();
_programList = programList;
}
private void frmSelectProgram_Load(object sender, EventArgs e)
{
foreach (string pro in _programList)
{
// MessageBox.Show(pro);
// for example fill a list box
}
}
}