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 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
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 ***
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
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
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
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]
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]
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
How about just:
Console.WriteLine(Path.GetDirectoryName("C:\temp\m yfile.txt") This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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
|
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,...
|
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
...
|
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"....
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |