locked
Problem with use of function GetFileName after OpenFileDialog

    Question

  • I don't seem to be able to find the function GetFileName in VB 2010 Express. I need to save the file name from a file path after an OpenFileDialog. Is this function enabled in of VB Express or do I have to define it first somewhere in the program?

    If so what would the code look like?

    Any advice please?

     

     

     

    Thursday, September 15, 2011 1:18 PM

Answers

  • Hello lumsoft,

    Thank you for your reply - I understood it very well, thank you.

    The VB code you have in your reply will return the full path of the selected file.

    for example it will return          c:\mydir\my documents\myfile.jpg      in field "path"

    What I want to do is return only myfile.jpg in a field

    I think a function GetFileName will do this but I cannot find this function in VB 2010 Express ??

     


    yes , i think understand now, you can use SafeFileName property of OpemFileDialog

     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
                Dim path As String = Me.OpenFileDialog1.SafeFileName
                Me.TextBox1.Text = path
            End If
        End Sub
    
    


    Regards.


    Carmelo La Monica

    Blog http://community.visual-basic.it/carmelolamonica/

    WordPress http://carmelolamonica.wordpress.com/

    Twitter  http://twitter.com/carmelolamonica

    Thursday, September 15, 2011 2:19 PM

All replies

  • Hello lumsoft,

    I don't seem to be able to find the function GetFileName in VB 2010 Express. I need to save the file name from a file path after an OpenFileDialog. Is this function enabled in of VB Express or do I have to define it first somewhere in the program?

    If so what would the code look like?

    Any advice please?

     

     

     


    I hope you understand because of my bad English

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
                Dim path As String = Me.OpenFileDialog1.FileName
                Me.Text = path
            End If
        End Sub
    
    


    Regards.


    Carmelo La Monica

    Blog http://community.visual-basic.it/carmelolamonica/

    WordPress http://carmelolamonica.wordpress.com/

    Twitter  http://twitter.com/carmelolamonica

    Thursday, September 15, 2011 1:53 PM
  • Thank you for your reply - I understood it very well, thank you.

    The VB code you have in your reply will return the full path of the selected file.

    for example it will return          c:\mydir\my documents\myfile.jpg      in field "path"

    What I want to do is return only myfile.jpg in a field

    I think a function GetFileName will do this but I cannot find this function in VB 2010 Express ??

     

    Thursday, September 15, 2011 2:06 PM
  • Hello lumsoft,

    Thank you for your reply - I understood it very well, thank you.

    The VB code you have in your reply will return the full path of the selected file.

    for example it will return          c:\mydir\my documents\myfile.jpg      in field "path"

    What I want to do is return only myfile.jpg in a field

    I think a function GetFileName will do this but I cannot find this function in VB 2010 Express ??

     


    yes , i think understand now, you can use SafeFileName property of OpemFileDialog

     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
                Dim path As String = Me.OpenFileDialog1.SafeFileName
                Me.TextBox1.Text = path
            End If
        End Sub
    
    


    Regards.


    Carmelo La Monica

    Blog http://community.visual-basic.it/carmelolamonica/

    WordPress http://carmelolamonica.wordpress.com/

    Twitter  http://twitter.com/carmelolamonica

    Thursday, September 15, 2011 2:19 PM
  • That works perfectly.

    Grazie Mille !

    Thursday, September 15, 2011 3:55 PM
  • Hello lumsoft,

    That works perfectly.

    Grazie Mille !


    You're Welcome.

    Best Regards.


    Carmelo La Monica

    Blog http://community.visual-basic.it/carmelolamonica/

    WordPress http://carmelolamonica.wordpress.com/

    Twitter  http://twitter.com/carmelolamonica

    Thursday, September 15, 2011 6:53 PM
  •       If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
             Dim Filename = IO.Path.GetFileName(ofd.FileName)
          End If
    
    

    ofd is the OpenFileDialog.
    Armin
    • Proposed as answer by Blackwood Monday, September 26, 2011 6:37 PM
    Monday, September 26, 2011 3:49 PM