我使用的是 Windows 10。
当我尝试使用 Powershell 创建文件时,出现以下错误
(注意:同样的行为发生在 cmd.exe 中,但不是 Git Bash)
PS C:\Users\John\Documents> New-Item hi.txt -type file
New-Item : Could not find file 'C:\Users\John\Documents\hi.txt'.
At line:1 char:1
+ New-Item hi.txt -type file
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError:
(C:\Users\John\Documents\hi.txt:String) [New-Item],
FileNotFoundException
+ FullyQualifiedErrorId : NewItemIOError,Microsoft.PowerShell.Commands.
NewItemCommand
我可以从文件资源管理器和 Git Bash 正常创建、打开和更新文件。
我已经检查了该目录的权限级别,并且我确实拥有该目录的完全控制权。
PS C:\Users\John> (get-acl .\Documents\).access
FileSystemRights : FullControl
AccessControlType : Allow
IdentityReference : NT AUTHORITY\SYSTEM
IsInherited : False
InheritanceFlags : ContainerInherit, ObjectInherit
PropagationFlags : None
FileSystemRights : FullControl
AccessControlType : Allow
IdentityReference : BUILTIN\Administrators
IsInherited : False
InheritanceFlags : ContainerInherit, ObjectInherit
PropagationFlags : None
FileSystemRights : FullControl
AccessControlType : Allow
IdentityReference : BUILTIN\Users
IsInherited : False
InheritanceFlags : None
PropagationFlags : None
FileSystemRights : FullControl
AccessControlType : Allow
IdentityReference : JOHN-DEEPBOOK\JOHN
IsInherited : False
InheritanceFlags : ContainerInherit, ObjectInherit
PropagationFlags : None
FileSystemRights : FullControl
AccessControlType : Allow
IdentityReference : NT AUTHORITY\SYSTEM
IsInherited : True
InheritanceFlags : ContainerInherit, ObjectInherit
PropagationFlags : None
FileSystemRights : FullControl
AccessControlType : Allow
IdentityReference : BUILTIN\Administrators
IsInherited : True
InheritanceFlags : ContainerInherit, ObjectInherit
PropagationFlags : None
FileSystemRights : FullControl
AccessControlType : Allow
IdentityReference : JOHN-DEEPBOOK\JOHN
IsInherited : True
InheritanceFlags : ContainerInherit, ObjectInherit
PropagationFlags : None
尝试以管理员身份运行 PowerShell,但发生了同样的情况。
这不是唯一存在此问题的目录。/Desktop 和/Contacts 也有同样的问题。我认为这些内置目录可能有一些特殊的东西,但/Downloads 工作正常。它们都具有相同的权限级别。
我发现的另一个有趣的行为是,当我从 PowerShell 调用 Sublime 时,它遇到了无法保存文件的相同问题。但是当我从开始菜单调用它时,它工作得很好。
我也尝试过在 Vscode 中编辑,当我尝试保存文件时它崩溃了。当我从 PowerShell 和开始菜单调用时,它都崩溃了。
这似乎更多是 Windows 问题,而不仅仅是 PowerShell,因为我在 cmd.exe 和 Vscode 上也遇到了同样的问题。
如果您知道为什么会发生这种情况,欢迎提出任何建议!!
最佳答案
我刚刚偶然发现了同样的问题,似乎受控文件夹访问会默默地阻止 pwsh.exe 写入文档或以其他方式访问它(默默地如:不会出现典型的“访问被拒绝”通知)。
如果您进入受控文件夹访问的“保护历史记录” Pane ,您将看到授权给 pwsh.exe 的 block 。允许该行为,powershell 将按预期工作。
关于windows - 无法在 cmd、Powershell 或 Vscode 中创建或更新文件。但我可以通过 Sublime、git bash 和文件资源管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53908202/