By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,137 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

Retrieving the parent folder name

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.