Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai
How to get process name from browsed file name in vb.net.

ex.if i browse file using file opendialogbox and file name is money.txt, and i need to get process name like "notepad.exe"

if i browse zip file it will show process name as "winRaR.exe"




regards
Aravind
Posted
Comments
[no name] 4-Sep-13 6:00am    
You would have to search the registry for the file association.

1 solution

Example of how to use GetRegisteredApplication
VB
Dim strProgramName As String = ""
Dim strShellAppName As String = ""
Dim strTestFileName As String = "C:\windows\CSPU.txt"
If GetRegisteredApplication(strTestFileName, _
    Path.GetExtension(strTestFileName), _
    strProgramName, strShellAppName) Then
    MsgBox("Program Name: " & strProgramName & vbNewLine & _
        "Shell Application Name: " & strShellAppName)
Else
    MsgBox("Association for " & strTestFileName & " was not found.")
End If


GetRegisteredApplication Function
VB
Public Function GetRegisteredApplication(ByVal ParamFileName As String, _
 ByVal FileExtension As String, _
 ByRef AppName As String, ByRef ShellAppName As String) As Boolean
    '
    ' Return registered application by file's extension
    '
    Dim StrExt As String
    Dim StrProgID As String
    Dim StrExe As String
    Dim oHKCR As RegistryKey ' HKEY_CLASSES_ROOT
    Dim oProgID As RegistryKey
    Dim oOpenCmd As RegistryKey
    Dim TempPos As Integer

    Try
        ' Add starting dot to extension
        If FileExtension.StartsWith(".") Then
            StrExt = FileExtension
        Else
            StrExt = "." & FileExtension
        End If
        ' Get Programmatic Identifier for this extension
        Try
            oHKCR = Registry.ClassesRoot
            oProgID = oHKCR.OpenSubKey(StrExt)
            StrProgID = oProgID.GetValue(Nothing).ToString
            oProgID.Close()
        Catch
            ' No ProgID, return false
            Return False
        End Try
        ' Get associated application
        Try
            oOpenCmd = oHKCR.OpenSubKey(StrProgID & "\shell\open\command")
            StrExe = oOpenCmd.GetValue(Nothing).ToString
            oOpenCmd.Close()
        Catch
            ' Missing default application
            Return False
        End Try
        TempPos = StrExe.IndexOf(" %1")
        If TempPos > 0 Then
            ' Replace %1 placeholder with ParamFileName
            StrExe = StrExe.Substring(0, TempPos)
            AppName = StrExe
            StrExe = StrExe & " " & Convert.ToChar(34) & _
                ParamFileName & Convert.ToChar(34)
            ShellAppName = StrExe
        Else
            ' No %1 placeholder found, append ParamFileName
            AppName = StrExe
            ShellAppName = StrExe & " " & Convert.ToChar(34) & _
                ParamFileName & Convert.ToChar(34)
        End If
        Return True
    Catch ex As Exception
        Return False
    End Try

End Function


Test output
The messagebox contains:
Quote:
Program Name: C:\Windows\system32\NOTEPAD.EXE
Application Name: C:\Windows\system32\NOTEPAD.EXE "C:\windows\CSPU.txt"
 
Share this answer
 
v8
Comments
Aravindba 6-Sep-13 0:00am    
very nice it will work,thanx a lot.....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900