FindFirstFileEx doesn't operate case sensitive

孤人 提交于 2019-12-24 03:06:08

问题


Since I'm using a macro which seems to work if the given path is case unequal to the local path on the drive I first need to validate wether the path is case-wise existing or not. Unfortunately (in my case) Directory.Exists() is not case sensitive.

So I tried FindFirstFileEx with dwAdditionalAttributes set to 1 which stands for FIND_FIRST_EX_CASE_SENSITIVE. However it seems not work for me. My local path is C:\Dir1\Dir2\Dir3. The path I compare is C:\dir1\Dir2\Dir3. Unfortunately I always get Dir3 as a result. I would have expected an empty result if the cases don't match.

What is my fault?

string dir = @"C:\Dir1\Dir2\Dir3" + '\0';
int FIND_FIRST_EX_CASE_SENSITIVE = 1;
WIN32_FIND_DATA fi;

IntPtr h = FindFirstFileEx( dir,
                            FINDEX_INFO_LEVELS.FindExInfoStandard,
                            out fi,
                            FINDEX_SEARCH_OPS.FindExSearchNameMatch,
                            IntPtr.Zero,
                            FIND_FIRST_EX_CASE_SENSITIVE);

回答1:


This functionality depends on value of registry key HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\kernel\obcaseinsensitive being set set to 0, which is not the default.

In other words, it depends on underlying file system settings, not the API itself.

more details here: http://www.siao2.com/2010/12/08/10101148.aspx




回答2:


By default, file searches in Windows are always case insensitive, regardless of the use of FIND_FIRST_EX_CASE_SENSITIVE.

To change this, you have to change a value in the registry (as far as I know).

See here for the gory details:

http://www.nicklowe.org/2012/02/understanding-case-sensitivity-in-windows-obcaseinsensitive-file_case_sensitive_search/

In your case, I expect

HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Kernel, REG DWORD obcaseinsensitive is set to 1



来源:https://stackoverflow.com/questions/16565979/findfirstfileex-doesnt-operate-case-sensitive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!