如何在Windows上访问位于具有保留系统名称的文件夹中的本地文件,如"CON"?

Kie*_*vit 2 windows filesystems powershell cmd path

我需要访问位于包含已命名文件夹的本地路径下的文件con.保留用于特定系统任务的文件夹名称等con,nul并尝试访问此类目录下的文件将导致以下消息:

  • 命令提示符:"系统找不到指定的路径."
  • 从文件资源管理器:"无法访问"C:\ Users\USERNAME\con".句柄无效.

如果我无法更改此类文件夹的名称,我该如何访问这些文件?

我注意到可以使用以下命令在命令提示符中创建此类文件夹名称:

md con\
Run Code Online (Sandbox Code Playgroud)

并可以删除:

rd /q /s con
Run Code Online (Sandbox Code Playgroud)

此外,可以使用资源管理器将文件拖入文件夹,从而成功添加文件.

我也尝试使用git-bash来创建,删除和打开这些目录的内容,但是一切看起来都很好但是,当我从Windows尝试时,即使文件显然在路径中也无法访问文件.

Rem*_*mko 5

使用此处\\?\所述的前缀,它应该在PowerShell中正常工作,如本所示.

PS C:\temp\posh> gci
PS C:\temp\posh> cmd /c "md con\"
PS C:\temp\posh> gci


    Directory: C:\temp\posh


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         12-06-18     09:24                con


PS C:\temp\posh> rm -LiteralPath '\\?\c:\temp\posh\con\'
PS C:\temp\posh> gci
PS C:\temp\posh>
Run Code Online (Sandbox Code Playgroud)

以下是如何使用PowerShell在具有保留名称的文件夹中编写和读取文件:

PS C:\temp\posh> "test" | out-file -LiteralPath "\\?\c:\temp\posh\con\test.txt"
PS C:\temp\posh> Get-Content -LiteralPath "\\?\c:\temp\posh\con\test.txt"
test
PS C:\temp\posh>
Run Code Online (Sandbox Code Playgroud)