Partager via


PathUtil.Normalize(String) Method

Definition

Returns a normalized form of the given path.

What it does:

  • Takes pains not to allocate extra temporary strings.
  • Verifies that the path contains no invalid path characters, throwing an exception if any invalid characters are found.
  • Strips leading and trailing whitespace.
  • Replaces "/" with ""
  • Replaces multiple consecutive "" with a single "", except for the leading "\" of a UNC path.
  • Removes the trailing "", unless it's part of a root (e.g. "C:")
  • Converts to lowercase

What it doesn't do:

  • Fully qualify a non-rooted filename (e.g. "dir\filename.ext")
  • Simplify relative pathnames (e.g. "C:\dir..\FileInDriveRoot.ext"). It's tempting to use Path.GetFullPathName to perform this function, but that would entail an expensive trip to the disk or network.
  • Verify that the path exists.
public:
 static System::String ^ Normalize(System::String ^ path);
public:
 static Platform::String ^ Normalize(Platform::String ^ path);
 static std::wstring Normalize(std::wstring const & path);
public static string Normalize (string path);
static member Normalize : string -> string
Public Function Normalize (path As String) As String

Parameters

path
String

Path to normalize

Returns

Normalized from of path

Remarks

What it does:

  • Takes pains not to allocate extra temporary strings

  • Verifies that the path contains no invalid path characters, throwing an exception if any invalid characters are found

  • Strips leading and trailing whitespace

  • Replaces "/" with "\"

  • Replaces multiple consecutive "\" with a single "\", except for the leading "\\" of a UNC path

  • Removes the trailing "\", unless it's part of a root (e.g. "C:\")

  • Converts to lowercase

What it doesn't do:

  • Fully qualify a non-rooted filename (e.g. "dir\filename.ext")

  • Simplify relative pathnames (e.g. "C:\dir\..\FileInDriveRoot.ext"). It's tempting to use Path.GetFullPathName to perform this function, but that would entail an expensive trip to the disk or network.

  • Verify that the path exists.

Applies to