|
How to retrieve full directory name from a file in VB.net |
|
|
|
|
Written by J. Bijvoets
|
|
Friday, 08 January 2010 19:09 |
Retrieve the directory for a file in Visual Basic.net
If you need to know in which directory a particular file is, you can choose to do it easy or the hard way.
How to retrieve the full path for a file in Visual Basic - The Hard Way
To find the directory for a file in a do-it-yourself way, you could do the following:
- Write a function to reverse a string
- Feed this function with the full name of the file (path + filename)
- Search for the first appearance of a backslash in the result of step 2. Call this position N.
- Strip the first N characters from the result from step 2
- Reverse the result of step 4. This is the full path.
or: Replace step 4 and 5 with: Strip the last N characters from the full name of the path.
As good developer I'm lazy if I can. So I only thought about how you could find the full path to a file in Visual Basic. I didn't write the code. It is better to do it the way Microsoft has designed Visual Basic.
Retrieving the directory from a file in Visual Basic - The smart way
Dim fil As String = "c:\test\dir1\example.txt" Dim MyInfo As New FileInfo(fil) MsgBox(MyInfo.DirectoryName)
This code shows "c:\test\dir1".
The FileInfo object can provide you with more information about a file, like size in bytes, dates of creation, last access and last modification.
|
|
Last Updated on Friday, 08 January 2010 19:32 |