JL.
JL.

Reputation: 81342

.net, built in way to get directory name from path?

Given the following directory:

string fullpath = "C:\MyDir1\MyDir2\MyDir3";

I would like to return "MyDir3" - this being the directory name (not full path, of a directory) , I know I can do this using string manipulation, but is there an easy (built in way) to achieve this using framework classes?

Thanks

Upvotes: 10

Views: 2241

Answers (3)

erikkallen
erikkallen

Reputation: 34421

string s = System.IO.Path.GetFileName(@"C:\MyDir1\MyDir2\MyDir3")

Upvotes: 0

hallie
hallie

Reputation: 2845

try this

string s =new  System.IO.DirectoryInfo(@"C:\MyDir1\MyDir2\MyDir3").Name;

Upvotes: 2

Thomas Levesque
Thomas Levesque

Reputation: 292675

string dir = new DirectoryInfo(fullpath).Name;

Upvotes: 23

Related Questions