1

I'm using the following method to open a file:

Process.Start();

This throws Win32Exception if a particular file with some extension is associated to a program in Windows that no longer exists. Now I could just simply do

 try{
      Process.start();
 } catch( Exception ex){
      // Error handling code
 }

however at the request of my employer I've been asked not to do this. Instead I would like to approach this by checking to see if the associated program/path_to_program exists, and if not pop a window telling the user to pick a program to run the file.

Thanks

Dr.Knowitall
  • 10,080
  • 23
  • 82
  • 133
  • 3
    Related: http://stackoverflow.com/questions/6679385/how-to-get-recommended-programs-associated-with-file-extension-in-c-sharp – George Johnston Apr 12 '13 at 20:32
  • 1
    There are lots of reasons you couldn't start a program. Like not having sufficient permissions, the file being corrupted, a DLL missing, etc. Using try/catch cannot be avoided, so you might as well use it. – Hans Passant Apr 12 '13 at 23:39
  • But in particular, its a Win32Exception that is being thrown. A file association is not being made. – Dr.Knowitall Apr 12 '13 at 23:46

1 Answers1

2

Create two forms and use some codes like this:

Inside the main form:

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
    }
}

and use a method like below to find the file (the extension) associated to witch programs: (this method is wrote by @LarsTech and i change some lines of them.)

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;
}

inside frmSelectProgram form:

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
        }
    }
}
Community
  • 1
  • 1
Ramin Bateni
  • 16,499
  • 9
  • 69
  • 98