Getting the last directory from a directory path

Posted on 2006-04-28
Last Modified: 2010-04-16
I'm using a folder browser to get a path to a directory that I will be compressing.

For eg C:\\Program File\Visual Studio.

As well as this I want to read the last directory of the path into a in this case Visual Studio as this will be the name of the compressed file.

The code I have to get the path is:
//set source path of snapshot
snapshotSource = _snapshotFolderBrowser.SelectedPath;

//now I need the code for
string compressedFileName = ???

Can someone give me some pointers to go about this please....


Question by:scm0sml
    LVL 30

    Accepted Solution


    This will give you the last dir:

                System.IO.DirectoryInfo pDirInfo = new System.IO.DirectoryInfo("path");
                System.IO.DirectoryInfo lastDirInfo =
                    pDirInfo.GetDirectories()[pDirInfo.GetDirectories().Length - 1];

    The GetDirectories Method have 2 overloads that can give you the ability to provide a search query.

    Alex :p
    LVL 30

    Expert Comment

    by:Alexandre Simões
    This may prevent errors:

                System.IO.DirectoryInfo pDirInfo = new System.IO.DirectoryInfo("path");
                System.IO.DirectoryInfo lastDirInfo =
                    (pDirInfo.GetDirectories()==null || pDirInfo.GetDirectories().Length==0) ? null : pDirInfo.GetDirectories()[pDirInfo.GetDirectories().Length - 1];

    Alex :p

    Author Comment

    yep working...

    cheers for that!

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now