# VBForums CodeBank > CodeBank - Visual Basic .NET >  Get Filename without path/extension

## Spirited Machine

This little code will get you the name of a file without its preceding path or file extension. Handy for linking controls to files and other things.

Code:


```
Public Function GetFileName(ByVal filepath As String) As String

        'This Function Gets the name of a file without the path or extension.

        'Input:
        '   filepath - Full path/filename of file.
        'Return:
        '   GetFileName - Name of file without the extension.

        'Get indices of characters directly before and after filename
        Dim slashindex As Integer = filepath.LastIndexOf("\")
        Dim dotindex As Integer = filepath.LastIndexOf(".")

        GetFileName = filepath.Substring(slashindex + 1, dotindex - slashindex - 1)
    End Function
```

Example use:


```
Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            
            Dim files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
            Dim filepath As String = files(0)
            Dim filename As String = GetFileName(filepath)
            Dim thing As New Textbox
                thing.Name = "thing" & thingcount
                thing.Text = filename
                AddHandler thing.Click, AddressOf button_click
                Dim mousex As Integer = Windows.Forms.Cursor.Position.X - Me.Location.X
                Dim mousey As Integer = Windows.Forms.Cursor.Position.Y - Me.Location.Y
                thing.Location = New Point(mousex, mousey)
                thingcount+= 1
End Sub
```

----------


## JuggaloBrotha

You're going to hate this, but all versions of .Net since 2002 (the first version) has the System.IO.Path namespace that has all this in there already, and it's OS independent, meaning if it's running on Mono (linux or Mac, both of which don't use a "\" as the separater character)  it'll still work where yours wont.

System.IO.Path.GetExtension()
System.IO.Path.GetFileName()
System.IO.Path.GetFileNameWithoutExtension()

System.IO.Path.DirectorySeparatorChar

System.IO.Path

----------


## Spirited Machine

Haha, thanks for letting me know. When I was looking to do this I found some code made by someone else that used for loops, etc. I thought I was doing someone a favor by simplifying it down to a couple lines. Oh well.  :Big Grin:

----------

