469,644 Members | 1,746 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,644 developers. It's quick & easy.

Retrieving the parent folder name

Is there any method in the File, Directory, FileInfo, Path etc classes
that makes it easy to retrieve the folder name of the immediate parent
folder of a file? I can't see one, but maybe I've missed it in all the
various overlapping methods and properties. For example, I might have
the file:

C:\Folder1\Folder2\Folder3\MyFile.txt

I want to be able to retrieve Folder3 into a string, no backslashes
etc, just 'Folder3'.

Obviously I can do this with various string search functions on the
full file path, but is this the only option or might there be a more
elegant approach

JGD
Nov 21 '05 #1
4 6398
John Dann schrieb:
Is there any method in the File, Directory, FileInfo, Path etc classes
that makes it easy to retrieve the folder name of the immediate parent
folder of a file? I can't see one, but maybe I've missed it in all the
various overlapping methods and properties. For example, I might have
the file:

C:\Folder1\Folder2\Folder3\MyFile.txt

I want to be able to retrieve Folder3 into a string, no backslashes
etc, just 'Folder3'.

Obviously I can do this with various string search functions on the
full file path, but is this the only option or might there be a more
elegant approach

JGD

MsgBox(IO.Path.GetFileName(IO.Path.GetDirectoryNam e( _
"C:\Folder1\Folder2\Folder3\MyFile.txt") _
))

Armin
Nov 21 '05 #2
On Sun, 10 Jul 2005 13:25:39 +0200, Armin Zingler
<az*******@freenet.de> wrote:

MsgBox(IO.Path.GetFileName(IO.Path.GetDirectoryNa me( _
"C:\Folder1\Folder2\Folder3\MyFile.txt") _
))


The documentation isn't very clear but doesn't that return:

'C:\Folder1\Folder2\Folder3'

even though you might possibly expect GetDirectoryName to just return
the immediate folder name? I've still then got to do some string
searching to recover 'Folder3' so arguably I might as well do that
from the start.

JGD
Nov 21 '05 #3
John,
As Armin suggests I rely on System.IO.Path for all my path "searching"
needs, as this helps ensure that I follow all the rules for paths (such as
trailing \ or not, usage of \ or / as separators, & other esoteric rules
that a number of developers "roling their own" miss).

If you try Armin's code it works as he states! To see it in action, try the
following code:

Dim fileName As String = "C:\Folder1\Folder2\Folder3\MyFile.txt"
Debug.WriteLine(fileName, "fileName")

Dim directoryName As String = IO.Path.GetDirectoryName(fileName)
Debug.WriteLine(directoryName, "directoryName")

Dim parentName As String = IO.Path.GetFileName(directoryName)
Debug.WriteLine(parentName, "parentName")

The output will be:

fileName: C:\Folder1\Folder2\Folder3\MyFile.txt
directoryName: C:\Folder1\Folder2\Folder3
parentName: Folder3

Hope this helps
Jay

"John Dann" <ne**@prodata.co.uk> wrote in message
news:57********************************@4ax.com...
| Is there any method in the File, Directory, FileInfo, Path etc classes
| that makes it easy to retrieve the folder name of the immediate parent
| folder of a file? I can't see one, but maybe I've missed it in all the
| various overlapping methods and properties. For example, I might have
| the file:
|
| C:\Folder1\Folder2\Folder3\MyFile.txt
|
| I want to be able to retrieve Folder3 into a string, no backslashes
| etc, just 'Folder3'.
|
| Obviously I can do this with various string search functions on the
| full file path, but is this the only option or might there be a more
| elegant approach
|
| JGD
Nov 21 '05 #4
On Sun, 10 Jul 2005 08:05:32 -0500, "Jay B. Harlow [MVP - Outlook]"
<Ja************@msn.com> wrote:
As Armin suggests I rely on System.IO.Path for all my path "searching"
needs, as this helps ensure that I follow all the rules for paths (such as
trailing \ or not, usage of \ or / as separators, & other esoteric rules
that a number of developers "roling their own" miss).


OK thanks. I guess I was thinking of Path.GetFilename as a method that
knew what a filename was, whereas it just seems to be a particular
type of string function.

JGD
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Jason | last post: by
4 posts views Thread by Danny Tuppeny | last post: by
1 post views Thread by Glenn T. Kitchen | last post: by
1 post views Thread by Brian P | last post: by
5 posts views Thread by Randy Smith | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.