extract directory name out of filename

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Hi,

is there a way to extract the directory-name out of a filename that includes
the path ?

thnx
Chris
 
Hi,

Yes, the System.IO.Path class has a GetDirectoryName static method which you
can use to achieve this, eg:

String dir = Path.GetDirectoryName("C:\windows\system\user32.dll");

If you only wanted "system" from the above path rather than the full name of
the directory then try combining it with GetFileName, like this, although if
GetDirectoryName returns a terminating \ character then you would need to
remove that first:

String dirname =
Path.GetFileName(Path.GetDirectoryName("C:\windows\system\user32.dll"));


Jonathan Holmes
 
Back
Top