How can I get only file name without its path using Open File Dialog? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How can I get only file name without its path using Open File Dialog?

mabilalmirzamabilalmirza Posts: 12Member
I made a simple notepad and put all functionality. Now I need to put the current file name on the title bar of the notepad. When I get file name from [color=Blue]Open File Dialog[/color], [color=Red]it returns file name with its full path[/color], whereas, [color=Green]I only need file name (without path)[/color]. How can I do this?

Comments

  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    : I made a simple notepad and put all functionality. Now I need to put
    : the current file name on the title bar of the notepad. When I get
    : file name from [color=Blue]Open File Dialog[/color], [color=Red]it
    : returns file name with its full path[/color], whereas,
    : [color=Green]I only need file name (without path)[/color]. How can I
    : do this?
    :
    Easy! Check out this code:
    [Code]
    Dim filePath as String = "HiMomIAmCool.txt"

    Dim fileName as String = filePath.Split(".")(0)
    [/Code]

    Now if the file might have a period in the name before the extension this code works:
    [Code]
    Dim filePath as String = "OkThis.IsNot.SoCool.txt"
    Dim fnPeices() as String = FilePath.Split(".")
    Dim fileName as String = ""
    Dim i as integer = 0
    Do while i < fnPeices.count - 1
    fileName &= fnPeices(i) & "."
    i += 1
    Loop
    'This line is to chop off the final period
    fileName = fileName.Substring(0, f.Length - 1)
    [/Code]
  • mabilalmirzamabilalmirza Posts: 12Member
    : : I made a simple notepad and put all functionality. Now I need to put
    : : the current file name on the title bar of the notepad. When I get
    : : file name from [color=Blue]Open File Dialog[/color], [color=Red]it
    : : returns file name with its full path[/color], whereas,
    : : [color=Green]I only need file name (without path)[/color]. How can I
    : : do this?
    : :
    : Easy! Check out this code:
    : [Code]
    : Dim filePath as String = "HiMomIAmCool.txt"
    :
    : Dim fileName as String = filePath.Split(".")(0)
    : [/Code]
    :
    : Now if the file might have a period in the name before the extension
    : this code works:
    : [Code]
    : Dim filePath as String = "OkThis.IsNot.SoCool.txt"
    : Dim fnPeices() as String = FilePath.Split(".")
    : Dim fileName as String = ""
    : Dim i as integer = 0
    : Do while i < fnPeices.count - 1
    : fileName &= fnPeices(i) & "."
    : i += 1
    : Loop
    : 'This line is to chop off the final period
    : fileName = fileName.Substring(0, f.Length - 1)
    : [/Code]

    Thankyou for replying. This is also a new thing for me. But my problem has not been solved yet. Just look at the given code
    [code]Dim str As String

    If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    str = Me.OpenFileDialog1.FileName()
    MessageBox.Show(str, "File Name")
    End If[/code]

    When I run this code (by a button), Open File Dialog box is shown. I select a file and click [color=Blue]Open[/color]. It shows me full path of the file in a message box.

    e.g. I select a file named [color=Blue]testFile.txt[/color] and click [color=Blue]Open[/color]. A message box shows me it full path
    [color=Blue]C:Documents and SettingsAdministratorDesktop estFile.txt[/color]
    whereas I only need its name [color=Blue]testFile.txt[/color] not the full path.
    Muhammad Abdullah Bilal Mirza
  • supermathewsupermathew Posts: 2Member
    : : : I made a simple notepad and put all functionality. Now I need to put
    : : : the current file name on the title bar of the notepad. When I get
    : : : file name from [color=Blue]Open File Dialog[/color], [color=Red]it
    : : : returns file name with its full path[/color], whereas,
    : : : [color=Green]I only need file name (without path)[/color]. How can I
    : : : do this?
    : : :
    : : Easy! Check out this code:
    : : [Code]
    : : Dim filePath as String = "HiMomIAmCool.txt"
    : :
    : : Dim fileName as String = filePath.Split(".")(0)
    : : [/Code]
    : :
    : : Now if the file might have a period in the name before the extension
    : : this code works:
    : : [Code]
    : : Dim filePath as String = "OkThis.IsNot.SoCool.txt"
    : : Dim fnPeices() as String = FilePath.Split(".")
    : : Dim fileName as String = ""
    : : Dim i as integer = 0
    : : Do while i < fnPeices.count - 1
    : : fileName &= fnPeices(i) & "."
    : : i += 1
    : : Loop
    : : 'This line is to chop off the final period
    : : fileName = fileName.Substring(0, f.Length - 1)
    : : [/Code]
    :
    : Thankyou for replying. This is also a new thing for me. But my
    : problem has not been solved yet. Just look at the given code
    : [code]: Dim str As String
    :
    : If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    : str = Me.OpenFileDialog1.FileName()
    : MessageBox.Show(str, "File Name")
    : End If[/code]:
    :
    : When I run this code (by a button), Open File Dialog box is shown. I
    : select a file and click [color=Blue]Open[/color]. It shows me full
    : path of the file in a message box.
    :
    : e.g. I select a file named [color=Blue]testFile.txt[/color] and
    : click [color=Blue]Open[/color]. A message box shows me it full path
    : [color=Blue]C:Documents and
    : SettingsAdministratorDesktop estFile.txt[/color]
    : whereas I only need its name [color=Blue]testFile.txt[/color] not
    : the full path.

    also simple:


    [Code]
    Dim filePath as String = "c:OkThisIsNotSoCool.txt"
    Dim fnPeices() as String = FilePath.Split("")
    Dim fileName as String = ""

    fileName = fnPeices(fnPeices.Length - 1)
    [/Code]

    if im wrong shoot me



  • mabilalmirzamabilalmirza Posts: 12Member
    : : : : I made a simple notepad and put all functionality. Now I need to put
    : : : : the current file name on the title bar of the notepad. When I get
    : : : : file name from [color=Blue]Open File Dialog[/color], [color=Red]it
    : : : : returns file name with its full path[/color], whereas,
    : : : : [color=Green]I only need file name (without path)[/color]. How can I
    : : : : do this?
    : : : :
    : : : Easy! Check out this code:
    : : : [Code]
    : : : Dim filePath as String = "HiMomIAmCool.txt"
    : : :
    : : : Dim fileName as String = filePath.Split(".")(0)
    : : : [/Code]
    : : :
    : : : Now if the file might have a period in the name before the extension
    : : : this code works:
    : : : [Code]
    : : : Dim filePath as String = "OkThis.IsNot.SoCool.txt"
    : : : Dim fnPeices() as String = FilePath.Split(".")
    : : : Dim fileName as String = ""
    : : : Dim i as integer = 0
    : : : Do while i < fnPeices.count - 1
    : : : fileName &= fnPeices(i) & "."
    : : : i += 1
    : : : Loop
    : : : 'This line is to chop off the final period
    : : : fileName = fileName.Substring(0, f.Length - 1)
    : : : [/Code]
    : :
    : : Thankyou for replying. This is also a new thing for me. But my
    : : problem has not been solved yet. Just look at the given code
    : : [code]: : Dim str As String
    : :
    : : If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    : : str = Me.OpenFileDialog1.FileName()
    : : MessageBox.Show(str, "File Name")
    : : End If[/code]: :
    : :
    : : When I run this code (by a button), Open File Dialog box is shown. I
    : : select a file and click [color=Blue]Open[/color]. It shows me full
    : : path of the file in a message box.
    : :
    : : e.g. I select a file named [color=Blue]testFile.txt[/color] and
    : : click [color=Blue]Open[/color]. A message box shows me it full path
    : : [color=Blue]C:Documents and
    : : SettingsAdministratorDesktop estFile.txt[/color]
    : : whereas I only need its name [color=Blue]testFile.txt[/color] not
    : : the full path.
    :
    : also simple:
    :
    :
    : [Code]
    : Dim filePath as String = "c:OkThisIsNotSoCool.txt"
    : Dim fnPeices() as String = FilePath.Split("")
    : Dim fileName as String = ""
    :
    : fileName = fnPeices(fnPeices.Length - 1)
    : [/Code]
    :
    : if im wrong shoot me
    :
    :
    :
    :
    Thank you, sir!
    You have made everything simple for me.
    Actually I was very much busy last week. This is the reason for checking your response too late.
    Thank you again, and again, and again,and ......


  • gnkavithagnkavitha Posts: 2Member
    : : : : : I made a simple notepad and put all functionality. Now I need to put
    : : : : : the current file name on the title bar of the notepad. When I get
    : : : : : file name from [color=Blue]Open File Dialog[/color], [color=Red]it
    : : : : : returns file name with its full path[/color], whereas,
    : : : : : [color=Green]I only need file name (without path)[/color]. How can I
    : : : : : do this?
    : : : : :
    : : : : Easy! Check out this code:
    : : : : [Code]
    : : : : Dim filePath as String = "HiMomIAmCool.txt"
    : : : :
    : : : : Dim fileName as String = filePath.Split(".")(0)
    : : : : [/Code]
    : : : :
    : : : : Now if the file might have a period in the name before the extension
    : : : : this code works:
    : : : : [Code]
    : : : : Dim filePath as String = "OkThis.IsNot.SoCool.txt"
    : : : : Dim fnPeices() as String = FilePath.Split(".")
    : : : : Dim fileName as String = ""
    : : : : Dim i as integer = 0
    : : : : Do while i < fnPeices.count - 1
    : : : : fileName &= fnPeices(i) & "."
    : : : : i += 1
    : : : : Loop
    : : : : 'This line is to chop off the final period
    : : : : fileName = fileName.Substring(0, f.Length - 1)
    : : : : [/Code]
    : : :
    : : : Thankyou for replying. This is also a new thing for me. But my
    : : : problem has not been solved yet. Just look at the given code
    : : : [code]: : : Dim str As String
    : : :
    : : : If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    : : : str = Me.OpenFileDialog1.FileName()
    : : : MessageBox.Show(str, "File Name")
    : : : End If[/code]: : :
    : : :
    : : : When I run this code (by a button), Open File Dialog box is shown. I
    : : : select a file and click [color=Blue]Open[/color]. It shows me full
    : : : path of the file in a message box.
    : : :
    : : : e.g. I select a file named [color=Blue]testFile.txt[/color] and
    : : : click [color=Blue]Open[/color]. A message box shows me it full path
    : : : [color=Blue]C:Documents and
    : : : SettingsAdministratorDesktop estFile.txt[/color]
    : : : whereas I only need its name [color=Blue]testFile.txt[/color] not
    : : : the full path.
    : :
    : : also simple:
    : :
    : :
    : : [Code]
    : : Dim filePath as String = "c:OkThisIsNotSoCool.txt"
    : : Dim fnPeices() as String = FilePath.Split("")
    : : Dim fileName as String = ""
    : :
    : : fileName = fnPeices(fnPeices.Length - 1)
    : : [/Code]
    : :
    : : if im wrong shoot me
    : :
    : :
    : :
    : :
    : Thank you, sir!
    : You have made everything simple for me.
    : Actually I was very much busy last week. This is the reason for
    : checking your response too late.
    : Thank you again, and again, and again,and ......
    :
    :
    :
  • gnkavithagnkavitha Posts: 2Member
    : : : : : : I made a simple notepad and put all functionality. Now I need to put
    : : : : : : the current file name on the title bar of the notepad. When I get
    : : : : : : file name from [color=Blue]Open File Dialog[/color], [color=Red]it
    : : : : : : returns file name with its full path[/color], whereas,
    : : : : : : [color=Green]I only need file name (without path)[/color]. How can I
    : : : : : : do this?
    : : : : : :
    : : : : : Easy! Check out this code:
    : : : : : [Code]
    : : : : : Dim filePath as String = "HiMomIAmCool.txt"
    : : : : :
    : : : : : Dim fileName as String = filePath.Split(".")(0)
    : : : : : [/Code]
    : : : : :
    : : : : : Now if the file might have a period in the name before the extension
    : : : : : this code works:
    : : : : : [Code]
    : : : : : Dim filePath as String = "OkThis.IsNot.SoCool.txt"
    : : : : : Dim fnPeices() as String = FilePath.Split(".")
    : : : : : Dim fileName as String = ""
    : : : : : Dim i as integer = 0
    : : : : : Do while i < fnPeices.count - 1
    : : : : : fileName &= fnPeices(i) & "."
    : : : : : i += 1
    : : : : : Loop
    : : : : : 'This line is to chop off the final period
    : : : : : fileName = fileName.Substring(0, f.Length - 1)
    : : : : : [/Code]
    : : : :
    : : : : Thankyou for replying. This is also a new thing for me. But my
    : : : : problem has not been solved yet. Just look at the given code
    : : : : [code]: : : : Dim str As String
    : : : :
    : : : : If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    : : : : str = Me.OpenFileDialog1.FileName()
    : : : : MessageBox.Show(str, "File Name")
    : : : : End If[/code]: : : :
    : : : :
    : : : : When I run this code (by a button), Open File Dialog box is shown. I
    : : : : select a file and click [color=Blue]Open[/color]. It shows me full
    : : : : path of the file in a message box.
    : : : :
    : : : : e.g. I select a file named [color=Blue]testFile.txt[/color] and
    : : : : click [color=Blue]Open[/color]. A message box shows me it full path
    : : : : [color=Blue]C:Documents and
    : : : : SettingsAdministratorDesktop estFile.txt[/color]
    : : : : whereas I only need its name [color=Blue]testFile.txt[/color] not
    : : : : the full path.
    : : :
    : : : also simple:
    : : :
    : : :
    : : : [Code]
    : : : Dim filePath as String = "c:OkThisIsNotSoCool.txt"
    : : : Dim fnPeices() as String = FilePath.Split("")
    : : : Dim fileName as String = ""
    : : :
    : : : fileName = fnPeices(fnPeices.Length - 1)
    : : : [/Code]
    : : :
    : : : if im wrong shoot me
    : : :
    : : :
    : : :
    : : :
    : : Thank you, sir!
    : : You have made everything simple for me.
    : : Actually I was very much busy last week. This is the reason for
    : : checking your response too late.
    : : Thank you again, and again, and again,and ......
    : :
    : :
    : :
Sign In or Register to comment.