diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 1fccb4e07d5..7f80af1965a 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -1218,10 +1218,10 @@ private FileSystemInfo GetFileSystemItem(string path, ref bool isContainer, bool path = NormalizePath(path); FileInfo result = new FileInfo(path); + // FileInfo.Exists is always false for a directory path, so we check the attribute for existence. var attributes = result.Attributes; - // FileInfo.Exists is true for files but false for directories - // so we check attributes directly. - bool exists = (int)attributes != -1; + if ((int)attributes == -1) { /* Path doesn't exist. */ return null; } + bool hidden = attributes.HasFlag(FileAttributes.Hidden); isContainer = attributes.HasFlag(FileAttributes.Directory); @@ -1250,10 +1250,9 @@ private FileSystemInfo GetFileSystemItem(string path, ref bool isContainer, bool // also return the object if (!isContainer) { - if (exists && (!hidden || Force || showHidden || filterHidden || switchFilterHidden)) + if (!hidden || Force || showHidden || filterHidden || switchFilterHidden) { s_tracer.WriteLine("Got file info: {0}", result); - return result; } } @@ -1270,10 +1269,9 @@ private FileSystemInfo GetFileSystemItem(string path, ref bool isContainer, bool // if "Hidden" is specified in the attribute filter dynamic parameters // also return the object - if (exists && (isRootPath || !hidden || Force || showHidden || filterHidden || switchFilterHidden)) + if (isRootPath || !hidden || Force || showHidden || filterHidden || switchFilterHidden) { s_tracer.WriteLine("Got directory info: {0}", result); - return new DirectoryInfo(path); } }