Results 1 to 5 of 5

Thread: Get the program that will open a file

  1. #1

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Get the program that will open a file

    I want to find the full path of the program that will open a given file type. e.g. '.txt' is opened by c:\windows\system32\notepad.exe

    I've done a bit of searching but i keep just finding how to create an association, not read one. I've also tried looking in the registery, but it looks like all that's there is a link to the program ID or something.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Get the program that will open a file

    Try this:

    vb.net Code:
    1. Private Sub CheckExtensions()
    2.     Dim extensions As String() = {".txt", ".dwg", ".doc", ".sln", ".gif", ".fake"}
    3.     Dim exePath As String = String.Empty
    4.     For Each extension In extensions
    5.         Select Case Me.GetExecutablePath(extension, exePath)
    6.             Case ExecutablePathResults.ExtensionNotFound
    7.                 MessageBox.Show("The named extension does not exist or a file association does not exist.", extension)
    8.  
    9.             Case ExecutablePathResults.Success
    10.                 MessageBox.Show(exePath, extension)
    11.  
    12.             Case ExecutablePathResults.UnexpectedKeyStructure
    13.                 MessageBox.Show("The expected key structure was incorrect.", extension)
    14.  
    15.             Case ExecutablePathResults.ExtensionDoesNotNameAType
    16.                 MessageBox.Show("Extension does not name a file classification.", extension)
    17.  
    18.             Case ExecutablePathResults.ExpectedExePathIsBlank
    19.                 MessageBox.Show("The expected exe path is blank.", extension)
    20.  
    21.         End Select
    22.     Next
    23. End Sub
    24.  
    25. Public Function GetExecutablePath(ByVal extension As String, ByRef result As String) As ExecutablePathResults
    26.     Dim defaultKey As Microsoft.Win32.RegistryKey = _
    27.                         My.Computer.Registry.ClassesRoot.OpenSubKey(extension, False)
    28.     Dim defaultValue As String = String.Empty
    29.  
    30.     If defaultKey IsNot Nothing Then
    31.         defaultValue = defaultKey.GetValue(String.Empty, String.Empty).ToString()
    32.     Else
    33.         Return ExecutablePathResults.ExtensionNotFound
    34.     End If
    35.  
    36.     If defaultValue <> String.Empty Then
    37.         Dim resultKey As Microsoft.Win32.RegistryKey = My.Computer.Registry.ClassesRoot _
    38.                                         .OpenSubKey(String.Format("{0}\shell\open\command", _
    39.                                                                   defaultValue))
    40.         If resultKey IsNot Nothing Then
    41.             result = resultKey.GetValue(String.Empty, String.Empty).ToString()
    42.             If result = String.Empty Then
    43.                 Return ExecutablePathResults.ExpectedExePathIsBlank
    44.             Else
    45.                 result = System.Text.RegularExpressions.Regex.Match(result, _
    46.                                         "[A-Za-z]{1}:[^%""]+").Value
    47.             End If
    48.             Return ExecutablePathResults.Success
    49.         Else
    50.             Return ExecutablePathResults.UnexpectedKeyStructure
    51.         End If
    52.     Else
    53.         Return ExecutablePathResults.ExtensionDoesNotNameAType
    54.     End If
    55.  
    56. End Function
    57.  
    58. Public Enum ExecutablePathResults
    59.     Success = 0
    60.     ExtensionNotFound = 1
    61.     UnexpectedKeyStructure = 2
    62.     ExtensionDoesNotNameAType = 3
    63.     ExpectedExePathIsBlank = 4
    64. End Enum

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Get the program that will open a file

    I was trying to get you an answer, but then I disovered the answer that I knew worked wouldn't run on my new 64 bit system, which lead me into a whole flurry of emails to resolve that.

    Anyways, there is a FindExecutable API call, which returns this information. It is more or less the same thing as ForumAccount's sample code, because FindExecutable does do a registry lookup for the value.

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Get the program that will open a file

    I was looking at the FindExectuable API and I was going to post it, but I realized it needed a file of the actual type to exist which I thought might not be wanted.

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Get the program that will open a file

    True. It depends on the intent of the code usage.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width