Skip to content

Can no longer get file attributes from exclusively locked files #26216

@iSazonov

Description

@iSazonov

Problem

IO.File.GetAttributes() doesn't return attributes for exclusively locked file like c:\pagefile.sys
IO.File.Exist() doesn't found such file (return false).

Repo on PowerShell

#########
[IO.File]::GetAttributes("c:\pagefile.sys")

Exception calling "GetAttributes" with "1" argument(s): "The process cannot access the file 'c:\pagefile.sys' because it is being used by another process."
At line:1 char:1
+ [IO.File]::GetAttributes("c:\pagefile.sys")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : IOException


$error[0].exception.InnerException | fl -Force

Message        : The process cannot access the file 'c:\pagefile.sys' because it is being used by another process.
Data           : {}
InnerException :
TargetSite     : System.IO.FileAttributes GetAttributes(System.String)
StackTrace     :    at System.IO.FileSystem.GetAttributes(String fullPath)
                    at CallSite.Target(Closure , CallSite , Type , String )
HelpLink       :
Source         : System.IO.FileSystem
HResult        : -2147024864


#########
[IO.File]::Exists("c:\pagefile.sys")

false

In PowerShell repo we have a workaround:

  1. https://github.com/PowerShell/PowerShell/blob/5cbf1b43de52fe9025bf756990b1cc6eb4e694e9/src/System.Management.Automation/namespaces/FileSystemProvider.cs#L6994-L7013
  2. https://github.com/PowerShell/PowerShell/blob/5cbf1b43de52fe9025bf756990b1cc6eb4e694e9/src/System.Management.Automation/engine/Utils.cs#L989-L1029

Without the workaround PowerShell doesn't show the file:

dir c:\pagefile.sys -Hidden

but cmd.exe show the file:

C:\Users\sie>dir c:\pagefile.sys /AH
 Volume in drive C has no label.
 Volume Serial Number is 824F-BBDF

 Directory of c:\

04/18/2018  10:48 AM    11,811,160,064 pagefile.sys
               1 File(s) 11,811,160,064 bytes
               0 Dir(s)  236,808,941,568 bytes free

Suggestion

I believe the issue should be fixed in CoreFX here
https://github.com/dotnet/corefx/blob/f25eb288a449010574a6e95fe298f3ad880ada1e/src/System.IO.FileSystem/src/System/IO/FileSystem.Windows.cs#L203-L223

ERROR_SHARING_VIOLATION should be added.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions