Path.GetDirectoryName() throw exception when path is empty or has only white spaces...
authorSantiago Fernandez Madero <safern@microsoft.com>
Fri, 5 May 2017 18:12:21 +0000 (11:12 -0700)
committerGitHub <noreply@github.com>
Fri, 5 May 2017 18:12:21 +0000 (11:12 -0700)
* Throw exception when path is empty or has only white spaces

Commit migrated from https://github.com/dotnet/coreclr/commit/d9722358f905a6ed0edcb0fccded66bb6a64fa0c

src/coreclr/src/mscorlib/shared/System/IO/Path.cs

index b3a8783..9feb287 100644 (file)
@@ -76,19 +76,23 @@ namespace System.IO
         // "\\server\share").
         public static string GetDirectoryName(string path)
         {
-            if (path != null)
+            if (string.IsNullOrWhiteSpace(path))
             {
-                PathInternal.CheckInvalidPathChars(path);
-                path = PathInternal.NormalizeDirectorySeparators(path);
-                int root = PathInternal.GetRootLength(path);
+                if (path == null) return null;
+                throw new ArgumentException(SR.Arg_PathIllegal, nameof(path));
+            }
 
-                int i = path.Length;
-                if (i > root)
-                {
-                    while (i > root && !PathInternal.IsDirectorySeparator(path[--i])) ;
-                    return path.Substring(0, i);
-                }
+            PathInternal.CheckInvalidPathChars(path);
+            path = PathInternal.NormalizeDirectorySeparators(path);
+            int root = PathInternal.GetRootLength(path);
+
+            int i = path.Length;
+            if (i > root)
+            {
+                while (i > root && !PathInternal.IsDirectorySeparator(path[--i])) ;
+                return path.Substring(0, i);
             }
+            
             return null;
         }