extracting file name from path.
Hi, I used the openfiledialog to select a file and managed to get the full path to the selected file using openfiledialog.fileName() but now i am trying to extract only the file name. E.g C:\temp\hello.mp3 i want to extract the "Hello.mp3" only. How can it be done?
thanks alot!
Re: extracting file name from path.
VB Code:
Dim s As String = System.IO.Path.GetFileName(path)
Re: extracting file name from path.
VB Code:
Dim s As String = Microsoft.VisualBasic.Right(path, path.Length - path.LastIndexOf("\") - 1)
Re: extracting file name from path.
Hi hyper88,
you can use this Code to get the file name...
VB Code:
Dim FullPath, FileName As String
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
FullPath = OpenFileDialog1.FileName
MessageBox.Show(FullPath)
FileName = FullPath.Substring(FullPath.LastIndexOf("\") + 1)
MessageBox.Show(FileName)
End If
Hope this helps...
Regards,
Re: extracting file name from path.
Why would you use an alternative when there is a method called GetFileName that is thus self-documenting and is also neater code?
Re: extracting file name from path.
Quote:
Originally Posted by jmcilhinney
Why would you use an alternative when there is a method called GetFileName that is thus self-documenting and is also neater code?
Because it's safer to do string manipulation than using IO channels
Re: extracting file name from path.
Quote:
Originally Posted by ComputerJy
Because it's safer to do string manipulation than using IO channels
Thats both meaningless and stupid. GetFileName uses no "IO channels" whatever you imagine those to be, it just does string manipulation.
jmc is right.
Re: extracting file name from path.
IO.Path.GetFileName is plain string manipulation. And it is safer as it tests for invalid characters and invalid path length...
Re: extracting file name from path.
As stated above. Here's the actual implementation:
VB Code:
Public Shared Function GetFileName(ByVal path As String) As String
If (Not path Is Nothing) Then
Path.CheckInvalidPathChars(path)
Dim num1 As Integer = path.Length
Dim num2 As Integer = num1
Do While (--num2 >= 0)
Dim ch1 As Char = path.Chars(num2)
If (((ch1 = Path.DirectorySeparatorChar) OrElse (ch1 = Path.AltDirectorySeparatorChar)) OrElse (ch1 = Path.VolumeSeparatorChar)) Then
Return path.Substring((num2 + 1), ((num1 - num2) - 1))
End If
Loop
End If
Return path
End Function
Re: extracting file name from path.
Quote:
Originally Posted by wossname
Thats both meaningless and stupid. GetFileName uses no "IO channels" whatever you imagine those to be, it just does string manipulation.
jmc is right.
Sorry, I didn't know that. but thank you for your polite words anyway
Re: extracting file name from path.
Quote:
Originally Posted by jmcilhinney
As stated above. Here's the actual implementation:
VB Code:
Public Shared Function GetFileName(ByVal path As String) As String
If (Not path Is Nothing) Then
Path.CheckInvalidPathChars(path)
Dim num1 As Integer = path.Length
Dim num2 As Integer = num1
Do While (--num2 >= 0)
Dim ch1 As Char = path.Chars(num2)
If (((ch1 = Path.DirectorySeparatorChar) OrElse (ch1 = Path.AltDirectorySeparatorChar)) OrElse (ch1 = Path.VolumeSeparatorChar)) Then
Return path.Substring((num2 + 1), ((num1 - num2) - 1))
End If
Loop
End If
Return path
End Function
Dear jmcilhinney,
I tried to test your code but it’s given an error on (CheckInvalidPathChars, DirectorySeparatorChar, AltDirectorySeparatorChar, VolumeSeparatorChar)
saying “not member of String”??? any idea?
Regards,
Re: extracting file name from path.
Quote:
Originally Posted by phenom
Dear jmcilhinney,
I tried to test your code but it’s given an error on (CheckInvalidPathChars, DirectorySeparatorChar, AltDirectorySeparatorChar, VolumeSeparatorChar)
saying “not member of String”??? any idea?
Regards,
he was just posting how the io.path.getfilename function works(because someone didnt think it was string minipulation).
so dont add that function and just use
dim name as string = io.path.getfilename(path)
Re: extracting file name from path.
Dear high6,
thanks for the Info... :wave:
Regards,