Get Path of file

T

tshad

If I have a string with the whole path of a file, how do I get just the
path?

I can get the file by using Path.GetFile and the extension by
Path.GetExtension.

But I can't find anything to get get just the Path (like Path.GetPath).

Is there a way to do it without having to get the FileName and then use an
IndexOf on the filename and then substring to get the path?

Thanks,

Tom
 
J

Jeff Johnson

That was it.

I somehow thought that it would only give me the Directory Name not the
path.

And that's why we test....

But wait, it gets better! If you have a path that you KNOW ends in a
directory (not a file), like "C:\Program Files\Some App" then to get the
directory name only, you use...wait for it...GetFileName()!!
 
T

tshad

Jeff Johnson said:
And that's why we test....

Which I have. And use quite extensivily. But I never needed to get just
the Path.
But wait, it gets better! If you have a path that you KNOW ends in a
directory (not a file), like "C:\Program Files\Some App" then to get the
directory name only, you use...wait for it...GetFileName()!!

Very funny.

However, you're own example shows the problem.

GetFileName() gets you what??????

Not a path, not a path and a file name, not a Directory and a file name -
but lo and behold it gets you a "FILE NAME". :)

What does GetExtension() get you? Need I say it??? An Extension.

What does FullPath() get you???? The FullPath plus the File Name.

What does GetFileNameWithoutExtension() get you? As you might expect - the
file name minus the path and minus the extension.

So based on the above, what might you expect from GetDirectoryName() -
perhaps the Directory name the file is in???? Nothing there about a path.

Yet we have other paths: FullPath, PathRoot, TempPath - so I would expect
just a GetPath(). Wouldn't you??? :)

Thanks,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top