2

my code

var info = new ProcessStartInfo(path);
info.WorkingDirectory = path;
info.WindowStyle = ProcessWindowStyle.Maximized;
var p = Process.Start(info);

it will open a file (.pdf, but could be another type of file later) In some cases, there's no program installed for open it, so it shows me the windows message

Windows can't open this file

and offers me to search some program to open it.

How can I avoid this? I want to show another message. In try/catch it doesn't throw any kind of exceptions, so its not working.

I tried, if is only .pdf to detect if there is any program installed with this

var key = Registry.ClassesRoot.OpenSubKey(".pdf");
if(key != null)
//  Not installed!

but it's not working. I'm getting a key, but I've uninstalled Foxit reader (and also I've use CCleaner to remove conflicts key, but still have .pdf key)

Please help

Gonzalo.-
  • 12,512
  • 5
  • 50
  • 82
  • 1
    Look at this http://stackoverflow.com/questions/6679385/how-to-get-recommended-programs-associated-with-file-extension-in-c-sharp – Steve May 17 '13 at 20:25

1 Answers1

1

You could use the FindExecutable() API, but the file being checked must actually exist:

    [System.Runtime.InteropServices.DllImport("shell32.dll")]
    static extern IntPtr FindExecutable(string lpFile, string lpDirectory, StringBuilder lpResult);

    private void Button1_Click(System.Object sender, System.EventArgs e)
    {
        string path = @"C:\Users\Mike\Documents\SomeFile.xyz";
        string EXE = GetAssociatedExecutable(path);
        if (!string.IsNullOrEmpty(EXE))
        {
            MessageBox.Show(EXE, "Associated Executable");
        }
        else
        {
            MessageBox.Show("No Asssociated Executable for: " + path);
        }
    }

    private string GetAssociatedExecutable(string filename)
    {
        const int MAX_PATH = 260;
        if (System.IO.File.Exists(filename))
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder(MAX_PATH);
            FindExecutable(filename, null, sb);
            if (sb.Length > 0)
            {
                return sb.ToString();
            }
        }
        return "";
    }
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40