Windows API访问区分大小写的路径(Windows上的Ubuntu上的Bash) [英] Windows API to access case-sensitive paths (Bash-on-Ubuntu-on-Windows)
问题描述
Windows上的Bash-on-Ubuntu支持区分大小写的文件路径.这意味着我可以创建两个文件或目录,它们的名称仅大写不同.我在访问这些文件时遇到了问题.
Bash-on-Ubuntu-on-Windows supports case-sensitive file paths. This means that I can create two files or directories with names only differing in capitalization. I have issues accessing those files, though.
运行
bash -c "touch Magic ; mkdir magic ; echo Secret! > magic/secret"
在该目录中创建一个文件名Magic
,一个名为magic
的目录和一个文件名secret
.
Creates a file names Magic
, a directory named magic
and a file names secret
in that directory.
bash -c "ls -lR"
产生
.:
total 0
drwxrwxrwx 2 root root 0 Aug 23 10:37 magic
-rwxrwxrwx 1 root root 0 Aug 23 10:37 Magic
./magic:
total 0
-rwxrwxrwx 1 root root 8 Aug 23 10:37 secret
(我不确定为什么会得到root
,因为它不是默认用户,但这似乎与我的问题无关.)
(I am not sure why I get root
, as it is not the default user, but that does not seem relevant to my question.)
Windows资源管理器显示:
Windows Explorer shows:
现在,虽然bash
可以轻松访问目录中的magic/secret
文件,但是Windows似乎将目录和文件视为一个相同的文件.所以双击目录我得到一个目录名无效"错误
Now, while bash
can easily access the magic/secret
file in the directory, Windows seems to treat both the directory and the file as one and the same. So double-clicking the directory I get a "directory name invalid" error
当我打印出The directory name is invalid.
时,使用cd
的情况也一样.
Same goes for using cd
, as I get The directory name is invalid.
printed out.
是否有任何API允许我访问那些区分大小写的路径或创建它们?似乎常规Windows API在访问现有文件时会完全忽略字符大小写.
Are there any APIs that allow me to access those case-sensitive paths, or create them? It seems that regular Windows APIs ignore character case completely when accessing existing files.
推荐答案
区分大小写的路径可以在带有NTFS的Windows上使用,但需要一些额外的工作.
Case-sensitive paths can be used on Windows with NTFS, but it requires a bit of extra work.
首先,必须在系统范围内启用区分大小写功能.这是通过将HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\kernel\ dword:ObCaseInsensitive
注册表值设置为0
,然后重新启动系统来完成的.
我发现这部分这里.
First, case-sensitivity must be enabled system-wide. This is done by setting the HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\kernel\ dword:ObCaseInsensitive
registry value to 0
, then restarting the system.
I found this part here.
一旦启用了区分大小写功能,就可以对区分大小写的路径使用CreateFile
.为此,必须将FILE_FLAG_POSIX_SEMANTICS
作为dwFlagsAndAttributes
参数的一部分传递.从 msdn :
Once case-sensitivity is enabled, it is possible to use CreateFile
to with case-sensitive paths. To do that, you have to pass the FILE_FLAG_POSIX_SEMANTICS
as part of the dwFlagsAndAttributes
parameter. From msdn:
将根据POSIX规则进行访问.对于支持该命名的文件系统,这包括允许多个文件的名称(仅大小写不同).
Access will occur according to POSIX rules. This includes allowing multiple files with names, differing only in case, for file systems that support that naming.
我在这个答案中找到了这部分.
I found this part in this answer.
通过设置注册表设置和CreateFile
标志,我可以访问区分大小写的路径.
By setting the registry setting and the CreateFile
flag, I was able to access case-sensitive paths.
这篇关于Windows API访问区分大小写的路径(Windows上的Ubuntu上的Bash)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!