473,889 Members | 1,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 65875
Somthing like this you mean:

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

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\MyFil e.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.CreateSubdir ectory("SubDir" )
Console.WriteLi ne("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.Direc toryInfo("C:\My Directory\MySub Directory\MyFil eName.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.CreateSubdir ectory("SubDir" )
Console.WriteLi ne("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.Direc toryInfo("C:\My Directory\MySub Directory\MyFil eName.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.CreateSubdir ectory("SubDir" )
Console.WriteLi ne("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\MyFil e.txt")
Console.WriteLi ne (fi.DirectoryNa me)

--
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\MyFil e.txt")
Console.WriteLi ne (fi.DirectoryNa me)

--
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******** **********@TK2M SFTNGP14.phx.gb l...
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3113
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 path - 'C:/Test' - however I sometimes have a UNC path - '//remoteComputer/Test' - and IIS appears to choke when it's given that. So, is there any way of translating a UNC path to the physical path on the remote computer? ie: Translating '...
5
11064
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 created in the wrong folder on the hard drive. Before I discovered these files, .NET kept trying to create a new project with _1 following the project name. Deleting those files corrected that problem. I deleted the Virtual Directory, rebooted, and...
2
1737
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
3020
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, _ Optional varTitleForDialog As Variant) As Variant ' Here's an example that gets an Access database name. Dim strFilter As String Dim lngFlags As Long Dim varFileName As Variant
10
2807
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 returns the filename...and just the filename. Which is what I want. In IE, however, this returns the ENTIRE local user's system path to the file
5
8640
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". Same file path containing special characters works fine in one machine, but doesn't work in other. I am using following API function to get short file path. Declare Auto Function GetShortPathName Lib "kernel32" (ByVal lpszLongPath As
2
5265
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 in the text box of File control. But in the code behind when I try to get the FullFile path using file1.PostedFile.FilePath it is not giving the full path (c:\test\test.pdf). Instead it is giving only file name (test.pdf). If i try to do this on...
4
2513
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 $domXmlElement){ require '/dom_functions.php'; doSomething($domXmlElement); // from "dom_functions.php" }
4
1660
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 to properties and the path is under the row called Target. Currently the code i wrote cannot display the path. Can you tell me wat is the problem. I m currently using vb.net 2003. Thanks in advance Private Sub cmdGet_Path_Click(ByVal sender...
0
9961
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11188
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10789
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10889
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10441
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9606
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6028
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4647
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4251
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.