473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 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 65746
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Simon | last post by:
I wonder if someone can help with this - I 'm creating an IISVirtualDirectory, and setting its Path property to the physical path I want to reference. This works fine when I have a standard file...
5
by: David Webb | last post by:
The problem started when the Working Folder for a project was somehow set to the folder of another project. I set the correct working folder in VSS and deleted the .vbproj files that had been...
2
by: Teis Draiby | last post by:
(Using C#) Question 1: Is there an easy build-in support for determining wether a given file path is 1) A valid file path (Only legal characters) 2) The file exists 3) The path exists
3
by: Wayne Aprato | last post by:
I am using code kindly provided by the Access Web to capture a file path and insert it into a form. The relevant part of the code follows: Function GetOpenFile(Optional varDirectory As Variant,...
10
by: darrel | last post by:
I have an input type="file" field that I am using to accept a file upload. This works, but I'm having problems with the filename property. In firefox, this: MyInputField.postedfile.filename ...
5
by: Sakharam Phapale | last post by:
Hi All, I am using an API function, which takes file path as an input. When file path contains special characters (@,#,$,%,&,^, etc), API function gives an error as "Unable to open input file"....
2
by: Sridhar | last post by:
Hi, I have a web form where it has a <input type=file id=file1> control. I have an Upload button to upload the file. WHen I click on browse and select one file, it is showing the full file path...
4
by: ManWithNoName | last post by:
I was wondering if it is possible to derive the file path from a loaded dom xml object, for example: $doc = new DOMDocument(); $doc->load('/path/to/file.xml'); foreach($doc->childNodes as...
4
by: Coolfusion | last post by:
Hi there, I want the programe to search for the shortcut exe file on the desktop and display the file path of the file. To obtain the file path manually, right-click the shortcut exe file and go...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.