-
Jun 8th, 2006, 10:18 PM
#1
Thread Starter
Hyperactive Member
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!
-
Jun 8th, 2006, 10:25 PM
#2
Frenzied Member
Re: extracting file name from path.
VB Code:
Dim s As String = System.IO.Path.GetFileName(path)
-
Jun 9th, 2006, 02:30 AM
#3
Re: extracting file name from path.
VB Code:
Dim s As String = Microsoft.VisualBasic.Right(path, path.Length - path.LastIndexOf("\") - 1)
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 9th, 2006, 02:51 AM
#4
Addicted Member
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,
=======================================
If I helped you, Kindly Rate my post. Thanks
-----------
PHENOM
-
Jun 9th, 2006, 03:21 AM
#5
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?
-
Jun 9th, 2006, 05:37 AM
#6
Re: extracting file name from path.
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
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 9th, 2006, 06:17 AM
#7
Re: extracting file name from path.
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.
I don't live here any more.
-
Jun 9th, 2006, 06:22 AM
#8
Hyperactive Member
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...
-
Jun 9th, 2006, 06:37 AM
#9
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
-
Jun 9th, 2006, 07:16 AM
#10
Re: extracting file name from path.
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
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 9th, 2006, 08:44 AM
#11
Addicted Member
Re: extracting file name from path.
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,
=======================================
If I helped you, Kindly Rate my post. Thanks
-----------
PHENOM
-
Jun 9th, 2006, 01:15 PM
#12
Frenzied Member
Re: extracting file name from path.
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)
-
Jun 9th, 2006, 11:18 PM
#13
Addicted Member
Re: extracting file name from path.
Dear high6,
thanks for the Info...
Regards,
=======================================
If I helped you, Kindly Rate my post. Thanks
-----------
PHENOM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|