Repository: PowerShell/PowerShell
File: src/System.Management.Automation/namespaces/FileSystemProvider.cs (C#)

Code before:
            }
            catch (Exception e)
            {
                if ((e is ArgumentNullException) ||
                    (e is ArgumentException) ||
                    (e is System.Security.SecurityException) ||
                    (e is UnauthorizedAccessException) ||
                    (e is PathTooLongException) ||
                    (e is NotSupportedException))
                {
                    return false;
                }
                else
                {
                    throw;
                }
            }

            // .NET introduced a change where invalid characters are accepted https://learn.microsoft.com/en-us/dotnet/core/compatibility/2.1#path-apis-dont-throw-an-exception-for-invalid-characters
            // We need to check for invalid characters ourselves

            // Remove drive root first
            string pathWithoutDriveRoot = path.Substring(Path.GetPathRoot(path).Length);

            foreach (string segment in pathWithoutDriveRoot.Split(Path.DirectorySeparatorChar))
            {
                if (segment.IndexOfAny(Path.GetInvalidFileNameChars()) != -1)
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Gets the item at the specified path.
        /// </summary>
        /// <param name="path">
        /// A fully qualified path representing a file or directory in the
        /// file system.
        /// </param>
        /// <returns>
        /// Nothing.  FileInfo and DirectoryInfo objects are written to the
        /// context's pipeline.
        /// </returns>
        /// <exception cref="System.ArgumentException">
        ///     path is null or empty.
        /// </exception>
        protected override void GetItem(string path)
        {

Review comment (suggestion):
i suppose it may be a toss up between this and:
`pathWithoutDriveRoot.IndexOfAny(Path.GetInvalidFileNameChars().Except(Path.DirectorySeparatorChar)) != 1)`

i wondered if it might be faster to not split (but then, of course, there's the linq)

Code after:
            }
            catch (Exception e)
            {
                if ((e is ArgumentNullException) ||
                    (e is ArgumentException) ||
                    (e is System.Security.SecurityException) ||
                    (e is UnauthorizedAccessException) ||
                    (e is PathTooLongException) ||
                    (e is NotSupportedException))
                {
                    return false;
                }
                else
                {
                    throw;
                }
            }

            // .NET introduced a change where invalid characters are accepted https://learn.microsoft.com/en-us/dotnet/core/compatibility/2.1#path-apis-dont-throw-an-exception-for-invalid-characters
            // We need to check for invalid characters ourselves.  `Path.GetInvalidFileNameChars()` is a supserset of `Path.GetInvalidPathChars()`

            // Remove drive root first
            string pathWithoutDriveRoot = path.Substring(Path.GetPathRoot(path).Length);
            char[] invalidFileChars = Path.GetInvalidFileNameChars();

            foreach (string segment in pathWithoutDriveRoot.Split(Path.DirectorySeparatorChar))
            {
                if (segment.IndexOfAny(invalidFileChars) != -1)
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Gets the item at the specified path.
        /// </summary>
        /// <param name="path">
        /// A fully qualified path representing a file or directory in the
        /// file system.
        /// </param>
        /// <returns>
        /// Nothing.  FileInfo and DirectoryInfo objects are written to the
        /// context's pipeline.
        /// </returns>
        /// <exception cref="System.ArgumentException">
        ///     path is null or empty.
        /// </exception>
        protected override void GetItem(string path)