471,083 Members | 1,123 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,083 software developers and data experts.

How to Get File Path

I'm looking for a quick function that will give me just the path for a file.
For example if I have the string "C:\Temp\MyFile.txt", I want to just return
just the string "C:\Temp\". How is this done using VB.NET?

Thanks
Dustin
Nov 21 '05 #1
10 65250
Somthing like this you mean:

Dim file As String = "C:\Temp\MyFile.txt"
Dim path As String = file.Substring(0, file.LastIndexOf("\"))

I didn't try it but that should work.

Dustin Wilson wrote:
I'm looking for a quick function that will give me just the path for a file.
For example if I have the string "C:\Temp\MyFile.txt", I want to just return
just the string "C:\Temp\". How is this done using VB.NET?

Thanks
Dustin

Nov 21 '05 #2
Dim x As String = "c:\MyDir\MyFile.txt"
Dim y As String = x.Substring(0, x.LastIndexOf("\") + 1)

I might be duplicating some internal VB function...but if I am, I
haven't discovered it yet...
*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #3
I think you should use the directory info object, here an example:
__________________

Imports System
Imports System.IO

Public Class MoveToTest

Public Shared Sub Main()
' Make a reference to a directory.
Dim di As New DirectoryInfo("TempDir")

' Create the directory only if it does not already exist.
If di.Exists = False Then
di.Create()
End If

' Create a subdirectory in the directory just created.
Dim dis As DirectoryInfo = di.CreateSubdirectory("SubDir")
Console.WriteLine("The root path of '{0}' is '{1}'", dis.Name,
dis.Root)

' Delete the parent directory.
di.Delete(True)
End Sub 'Main
End Class 'MoveToTest
Nov 21 '05 #4
Casting the string into DirectoryInfo Class does auto parsing and validation.

An improved code snippet would be:

Dim ldiTemp as New
System.IO.DirectoryInfo("C:\MyDirectory\MySubDirec tory\MyFileName.txt")
'Check whether the file exists

If ldiTemp.Exists = True Then

lsFileName = ldiTemp.Name ' will return MyFileName.txt
lsDirectory = ldiTemp.Parent.FullName ' will return
C:\MyDirectory\MySubDirectory

End If


"poldoj" wrote:
I think you should use the directory info object, here an example:
__________________

Imports System
Imports System.IO

Public Class MoveToTest

Public Shared Sub Main()
' Make a reference to a directory.
Dim di As New DirectoryInfo("TempDir")

' Create the directory only if it does not already exist.
If di.Exists = False Then
di.Create()
End If

' Create a subdirectory in the directory just created.
Dim dis As DirectoryInfo = di.CreateSubdirectory("SubDir")
Console.WriteLine("The root path of '{0}' is '{1}'", dis.Name,
dis.Root)

' Delete the parent directory.
di.Delete(True)
End Sub 'Main
End Class 'MoveToTest

Nov 21 '05 #5
Check out the Path Class.
--
Dennis in Houston
"vvenk" wrote:
Casting the string into DirectoryInfo Class does auto parsing and validation.

An improved code snippet would be:

Dim ldiTemp as New
System.IO.DirectoryInfo("C:\MyDirectory\MySubDirec tory\MyFileName.txt")
'Check whether the file exists

If ldiTemp.Exists = True Then

lsFileName = ldiTemp.Name ' will return MyFileName.txt
lsDirectory = ldiTemp.Parent.FullName ' will return
C:\MyDirectory\MySubDirectory

End If


"poldoj" wrote:
I think you should use the directory info object, here an example:
__________________

Imports System
Imports System.IO

Public Class MoveToTest

Public Shared Sub Main()
' Make a reference to a directory.
Dim di As New DirectoryInfo("TempDir")

' Create the directory only if it does not already exist.
If di.Exists = False Then
di.Create()
End If

' Create a subdirectory in the directory just created.
Dim dis As DirectoryInfo = di.CreateSubdirectory("SubDir")
Console.WriteLine("The root path of '{0}' is '{1}'", dis.Name,
dis.Root)

' Delete the parent directory.
di.Delete(True)
End Sub 'Main
End Class 'MoveToTest

Nov 21 '05 #6
In article <#o**************@TK2MSFTNGP14.phx.gbl>, Dustin Wilson wrote:
I'm looking for a quick function that will give me just the path for a file.
For example if I have the string "C:\Temp\MyFile.txt", I want to just return
just the string "C:\Temp\". How is this done using VB.NET?

Thanks
Dustin

Imports System.IO

....

Dim fi As New FileInfo ("C:\Temp\MyFile.txt")
Console.WriteLine (fi.DirectoryName)

--
Tom Shelton [MVP]
Nov 21 '05 #7
Tom:

Yours is even simpler.

Venki

"Tom Shelton" wrote:
In article <#o**************@TK2MSFTNGP14.phx.gbl>, Dustin Wilson wrote:
I'm looking for a quick function that will give me just the path for a file.
For example if I have the string "C:\Temp\MyFile.txt", I want to just return
just the string "C:\Temp\". How is this done using VB.NET?

Thanks
Dustin

Imports System.IO

....

Dim fi As New FileInfo ("C:\Temp\MyFile.txt")
Console.WriteLine (fi.DirectoryName)

--
Tom Shelton [MVP]

Nov 21 '05 #8
Dustin,

As David already told, use the io.path class it has all kinds of methods to
give you everyting. It exist as well for Uri's.

http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps,

Cor
Nov 21 '05 #9
Wow.. Many different way's to skin the same cat. Thank you all for
responses quick responses.

Thanks
Dustin

"Dustin Wilson" <dw*****@REMOVE.kgsgroup.com> wrote in message
news:%2******************@TK2MSFTNGP14.phx.gbl...
I'm looking for a quick function that will give me just the path for a file. For example if I have the string "C:\Temp\MyFile.txt", I want to just return just the string "C:\Temp\". How is this done using VB.NET?

Thanks
Dustin

Nov 21 '05 #10
How about just:

Console.WriteLine(Path.GetDirectoryName("C:\temp\m yfile.txt")

Nov 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.