• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

Getting the last directory from a directory path

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 string...so 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....

Cheers



0
scm0sml
Asked:
scm0sml
  • 2
1 Solution
 
Alexandre SimõesManager / Technology SpecialistCommented:
Hi...

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
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
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
0
 
scm0smlAuthor Commented:
yep working...

cheers for that!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now