none
Is there an api to extract the image path from the command line? RRS feed

  • Question

  • I want to extact the file image path (true path) from the various command line? is there the api or function? thank you.
    Tuesday, May 15, 2018 12:16 AM

Answers

  • You can try something along the lines of the following:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'the test strings, as posted in thread
        Dim strs() = {"C:\PROGRA~1\MICROS~3\OFFICE11\OUTLOOK.EXE -c IPM.Note /m ""%1""",
        "C:\Program Files\Adobe\Photoshop CS\Photoshop /Automatio",
        """c:\windows\system32\rundll32.exe"" xxx.dll,xxxx"}
    
        Dim badPathChars = IO.Path.GetInvalidPathChars
    
        'loop through strings
        For Each s In strs
            'remove any invalid path characters
            For Each b In badPathChars
                s = s.Replace(b, "")
            Next
    
            'find last path seperator character
            Dim idx As Integer = s.LastIndexOf("\"c)
    
            'remove characters from end of string
            ' until a valid file path is found
            While idx < s.Length
                If Not IO.File.Exists(s) Then
                    s = s.Substring(0, s.Length - 1)
                Else
                    Exit While
                End If
            End While
    
            'if idx = s.length then the file was
            ' not found
            If idx = s.Length Then
                Dim lbl As New Label
                lbl.Text = "File not found " & s
                FlowLayoutPanel1.Controls.Add(lbl)
            Else
                'extract associated icon
                Dim ico = Icon.ExtractAssociatedIcon(s)
                'display icon in picturebox loaded into
                ' flow layout panel (for testing)
                Dim pbx As New PictureBox
                pbx.Image = ico.ToBitmap
                FlowLayoutPanel1.Controls.Add(pbx)
            End If
        Next
    End Sub
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by gaxjyxq Tuesday, May 15, 2018 9:15 PM
    Tuesday, May 15, 2018 12:05 PM
    Moderator

All replies

  • Without more details a direct reply can not be made.

    Yet you could look at System.IO.Path methods.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, May 15, 2018 12:20 AM
    Moderator
  • I want to extact the file image path (true path) from the various command line? is there the api or function? thank you.

    Hi gaxjyxq,

    What kind of command line? Can you give us one example that you want to do here?

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 15, 2018 2:56 AM
    Moderator
  • The command lines like below:

    1. C:\PROGRA~1\MICROS~3\OFFICE11\OUTLOOK.EXE -c IPM.Note /m "%1"

    2. C:\Program Files\Adobe\Photoshop CS\Photoshop /Automatio

    3. "c:\windows\system32\rundll32.exe" xxx.dll,xxxx

    etc.


    The various command lines are in Registry, thank you
    • Edited by gaxjyxq Tuesday, May 15, 2018 3:28 AM
    Tuesday, May 15, 2018 3:26 AM
  • Tuesday, May 15, 2018 4:40 AM
  • Check this Regular Expression:

    Dim example = "C:\PROGRA~1\MICROS~3\OFFICE11\OUTLOOK.EXE -c IPM.Note /m ""%1"""
    Dim result = Regex.Match(example, "(?<=^"")(\^""|.)*?(?="".*$)|^.*?(?=\s+[-/].*$)|^.*?(?=\s.*$)").Value
    Console.WriteLine(result)

    Or give details about the desired results.



    • Edited by Viorel_MVP Tuesday, May 15, 2018 5:01 AM
    Tuesday, May 15, 2018 5:00 AM
  • Thank you, but your regular expression is only for this command line(C:\PROGRA~1\MICROS~3\OFFICE11\OUTLOOK.EXE -c IPM.Note /m "%1)? are there the common APIs or classes or regular expressions for the various command lines?
    Tuesday, May 15, 2018 9:38 AM
  • Thank you, but your regular expression is only for this command line(C:\PROGRA~1\MICROS~3\OFFICE11\OUTLOOK.EXE -c IPM.Note /m "%1)? are there the common APIs or classes or regular expressions for the various command lines?

    There is no rule as to what a command line should look like, so it's always going to be something of a guess.   The best you are going to do is to fine tune that regex so it is as reliable as possible. For instance, are you only interested in the file path if it occurs at the start of the command line, or do you want all file paths?  Will the path always start with a capital letter and ":\" or are you also interested in network paths and relative paths?

    Tuesday, May 15, 2018 10:04 AM
  • Which result do you expect in each of the examples?
    Tuesday, May 15, 2018 11:30 AM
  • You can try something along the lines of the following:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'the test strings, as posted in thread
        Dim strs() = {"C:\PROGRA~1\MICROS~3\OFFICE11\OUTLOOK.EXE -c IPM.Note /m ""%1""",
        "C:\Program Files\Adobe\Photoshop CS\Photoshop /Automatio",
        """c:\windows\system32\rundll32.exe"" xxx.dll,xxxx"}
    
        Dim badPathChars = IO.Path.GetInvalidPathChars
    
        'loop through strings
        For Each s In strs
            'remove any invalid path characters
            For Each b In badPathChars
                s = s.Replace(b, "")
            Next
    
            'find last path seperator character
            Dim idx As Integer = s.LastIndexOf("\"c)
    
            'remove characters from end of string
            ' until a valid file path is found
            While idx < s.Length
                If Not IO.File.Exists(s) Then
                    s = s.Substring(0, s.Length - 1)
                Else
                    Exit While
                End If
            End While
    
            'if idx = s.length then the file was
            ' not found
            If idx = s.Length Then
                Dim lbl As New Label
                lbl.Text = "File not found " & s
                FlowLayoutPanel1.Controls.Add(lbl)
            Else
                'extract associated icon
                Dim ico = Icon.ExtractAssociatedIcon(s)
                'display icon in picturebox loaded into
                ' flow layout panel (for testing)
                Dim pbx As New PictureBox
                pbx.Image = ico.ToBitmap
                FlowLayoutPanel1.Controls.Add(pbx)
            End If
        Next
    End Sub
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by gaxjyxq Tuesday, May 15, 2018 9:15 PM
    Tuesday, May 15, 2018 12:05 PM
    Moderator
  • Thank you for your code, i will reference it.

    Tuesday, May 15, 2018 9:15 PM