Results 1 to 10 of 10

Thread: [RESOLVED] Getting application title using openFileDialog

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2010
    Posts
    26

    Resolved [RESOLVED] Getting application title using openFileDialog

    Currently, my problem is that I can't display the application name that I've chosen using openFileDialog. In this case, I'm not referring to the FileName where it displays the full path. I just want the last segment of the FileName, which is the targeted application name.

    Perhaps you can work on this code snippet:
    vb.net Code:
    1. Imports System.IO
    2. Imports System.Diagnostics
    3.  
    4. '
    5. Public Partial Class MainForm
    6.     Public Sub New()
    7.         ' The Me.InitializeComponent call is required for Windows Forms designer support.
    8.         Me.InitializeComponent()
    9.        
    10.         '
    11.         ' TODO : Add constructor code after InitializeComponents
    12.  
    13.     End Sub
    14.  
    15.     Sub Button1Click(ByVal sender As Object, ByVal e As EventArgs)
    16.         OpenFileDialog1.Title = "Please Select a File"
    17.          OpenFileDialog1.InitialDirectory = "C:temp"          
    18.  
    19.          OpenFileDialog1.ShowDialog()    
    20.     End Sub
    21.  
    22.     Sub OpenFileDialog1FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
    23.           Dim strm As System.IO.Stream
    24.          strm = OpenFileDialog1.OpenFile()
    25.          TextBox1.Text =  OpenFileDialog1.FileName.ToString()
    26.     End Sub
    27. 'This section displays the full path in a textbox. Then Process.Start event takes place at button2.
    28.  
    29.    
    30.     Sub Button2Click(ByVal sender As Object, ByVal e As EventArgs) 
    31.           Process.Start(OpenFileDialog1.FileName)
    32.            ' Below Process.Start, I want the name of the targeted application to be displayed in a label after this button click event.
    33.  
    34. End Sub
    35.  
    36. End Class[CODE][/CODE]

    And of course, thank you if you've taken the time to help me! (:

    EDIT: I've found some leads which I think can be used but I do not know how to go about applying it here.
    Links:
    http://www.freevbcode.com/ShowCode.asp?ID=5558
    http://bytes.com/topic/visual-basic-...w-get-filename

    EDIT2: Problem solved. (By adding this code snippet)
    Code:
    iPos = 1
    Do
    iPos = InStr(iPos, sPath, "\")
    
    If iPos = 0 Then Exit Do
    
    iPos = iPos + 1
    iPosSave = iPos - 1
    
    Loop
    label1.Text = (Trim(Mid(sPath, iPosSave + 1)))
    ]
    Last edited by metalm3; Aug 4th, 2010 at 12:34 PM.

  2. #2
    Addicted Member Spirited Machine's Avatar
    Join Date
    May 2009
    Posts
    215

    Re: Getting application title using openFileDialog

    io.path.getfilename(openfiledialog1.filename)
    or
    io.path.getfilenamewithoutextension(openfiledialog1.filename)

  3. #3
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: Getting application title using openFileDialog

    When dealing with things like this, it's best to read the documentation first.

    I wasn't sure, so I went to MSDN and typed in 'OpenFileDialog'

    I read through the properties and saw 'SafeFileName'

    Quote Originally Posted by MSDN
    Gets the file name and extension for the file selected in the dialog box. The file name does not include the path.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2010
    Posts
    26

    Re: Getting application title using openFileDialog

    Quote Originally Posted by Spirited Machine View Post
    io.path.getfilename(openfiledialog1.filename)
    or
    io.path.getfilenamewithoutextension(openfiledialog1.filename)
    Hey there. I've resolved the problem. But yours is a much faster and instant way! Thanks! (:

  5. #5
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: Getting application title using openFileDialog

    Quote Originally Posted by Spirited Machine View Post
    io.path.getfilename(openfiledialog1.filename)
    or
    io.path.getfilenamewithoutextension(openfiledialog1.filename)
    That creates unnecessary overhead. The OP should just use the 'SafeFileName' property of the OpenFileDialog.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jul 2010
    Posts
    26

    Re: Getting application title using openFileDialog

    Quote Originally Posted by weirddemon View Post
    When dealing with things like this, it's best to read the documentation first.

    I wasn't sure, so I went to MSDN and typed in 'OpenFileDialog'

    I read through the properties and saw 'SafeFileName'
    I did read that, but didn't pay much attention to that property. And I've learned my lesson. (: Thanks for the help!

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2010
    Posts
    26

    Re: Getting application title using openFileDialog

    Quote Originally Posted by weirddemon View Post
    That creates unnecessary overhead. The OP should just use the 'SafeFileName' property of the OpenFileDialog.
    I went to see the declaration code snippet but I'm not sure on how to apply it. What's the "Get" there for?

    vb.net Code:
    1. [CODE]'Declaration
    2.  
    3. <BrowsableAttribute(False)> _
    4. Public ReadOnly Property SafeFileName As String
    5.     Get
    6. [/CODE]

    EDIT:
    Oops. Mental block earlier on. Got it with OpenFileDialog1.SafeFileName. Thanks (:

  8. #8
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: Getting application title using openFileDialog

    Quote Originally Posted by metalm3 View Post
    I went to see the declaration code snippet but I'm not sure on how to apply it. What's the "Get" there for?

    vb.net Code:
    1. [CODE]'Declaration
    2.  
    3. <BrowsableAttribute(False)> _
    4. Public ReadOnly Property SafeFileName As String
    5.     Get
    6. [/CODE]

    EDIT:
    Oops. Mental block earlier on. Got it with OpenFileDialog1.SafeFileName. Thanks (:
    Great. Please mark the thread resolved by selecting it from the Thread Tools menu located near the top of the page.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  9. #9
    Addicted Member Spirited Machine's Avatar
    Join Date
    May 2009
    Posts
    215

    Re: Getting application title using openFileDialog

    Quote Originally Posted by weirddemon View Post
    That creates unnecessary overhead. The OP should just use the 'SafeFileName' property of the OpenFileDialog.
    Well I just learned something new today too. I didn't know that property existed. Thanks.

  10. #10
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: Getting application title using openFileDialog

    Quote Originally Posted by Spirited Machine View Post
    Well I just learned something new today too. I didn't know that property existed. Thanks.
    No problem. Like JMC 'says', when in doubt, read the documentation
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

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