PathStripPath (shlwapi)
Last changed: anfortas.geo@yahoo.com-216.204.61.86

.
Summary
Removes the path portion of a fully qualified path and file.

C# Signature:

[DllImport("shlwapi.dll", CharSet=CharSet.Auto)]
static extern void PathStripPath([In,Out] StringBuilder pszPath);

VB.NET Signature

''' <summary>
''' Removes the path portion of a fully qualified path and file.
''' </summary>
''' <param name="pszPath">A pointer to a null-terminated string of length MAX_PATH that contains the path and file name. When this function returns successfully, the string contains only the file name, with the path removed.</param>
<DllImport("shlwapi.dll", EntryPoint:="PathStripPathW",  SetLastError:=True, CharSet:=CharSet.Unicode)> _
Public Sub PathStripPath(<MarshalAs(UnmanagedType.LPTStr)>pszPath As System.Text.StringBuilder)
End Sub

VB Signature:

Public Declare Sub PathStripPath Lib "shlwapi" Alias "PathStripPathA" _
        (ByVal pszPath As String)

Notes:

This is similar, but not identical, to Path.GetFileName. If the input string ends with a directory, the directory is returned rather than nothing; for example, "C:\foo\" becomes "foo\", "C:" stays "C:".

Tips & Tricks:

Please add some!

Sample Code:

C# Example

StringBuilder str = new StringBuilder(@"c:\dir1\file.txt");
PathStripPath(str);
// Result: str.ToString() == "file.txt"

VB.NET Example

Dim strFile As New system.Text.StringBuilder(255)
strFile.Append("c:\abc\def\words.doc")
Win32.API.Shlwapi.PathStripPath(strFile)
System.Diagnostics.Debug.WriteLine(strFile.ToString())

Alternative Managed API:

Path.GetFileName

Documentation