How to Get File Path

D

Dustin Wilson

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
 
M

Matthew Hazlett

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.
 
T

Terry Olsen

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...
 
P

poldoj

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
 
G

Guest

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\MySubDirectory\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
 
G

Guest

Check out the Path Class.
--
Dennis in Houston


vvenk said:
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\MySubDirectory\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
 
T

Tom Shelton

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)
 
G

Guest

Tom:

Yours is even simpler.

Venki

Tom Shelton said:
Imports System.IO

....

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

Dustin Wilson

Wow.. Many different way's to skin the same cat. Thank you all for
responses quick responses.

Thanks
Dustin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top