WindowsXP资源管理器文件名排序规则- -

来源:互联网 发布:图片怎么在淘宝上搜索 编辑:程序博客网 时间:2024/05/16 06:23

本文大多摘录于网页,目的了解WindowsXP资源管理器文件名排序规则 和StrCmpLogicalW函数。

   在Windows XP与 Windows 2003 Server中,资源管理器使用了一种不同于Windows 2000的新的文件名、目录名排序规则,例如:

在Windows XP与Windows 2003 Server中:

    Ie4_01
    Ie4_128
    Ie5
    Ie6
    Ie401sp2
    Ie501sp2

在Windows 2000中:

    Ie4_01
    Ie4_128
    Ie401sp2
    Ie5
    Ie501sp2
    Ie6

 

   在默认方式下,Windows XP与 Windows 2003 Server把文件名和目录名中出现的数字按照数学中的数字大小进行比较,而不是按照字符串大小进行比较。

 所以,在上面Windows XP与Windows 2003 Server的例子中,在按照升序排列的情况下,Ie401sp2之所以会排在Ie6的后面,是因为数字401要大于6。

 

  更进一步,请看下面的例子:

在Windows XP与Windows 2003 Server中:
    5.txt
    11.txt
    88.txt
在Windows 2000中:
    11.txt
    5.txt
    88.txt
    那么如何改变Windows XP与 Windows 2003 Server这种默认的排序方式,而仍然继续使用Windows 2000中的排序规则呢?该规则存放在以
下注册表键中:
HKLM/Software/Microsoft/Windows/Currentversion/Policies/Explorer/NoStrCmpLogical
HKCU/Software/Microsoft/Windows/Currentversion/Policies/Explorer/NoStrCmpLogical
此键为DWORD类型,如果要使用Windows XP或Windows 2003 Server的文件名排序规则,则设置此键值为 0;如果要使用Windows 2000的文件名
排序规则,则设置此键值为 1。
注意:以上功能仅对Windows XP Service Pack 1及其后的版本有效。
可是,我们自己如何实现这种排序呢?可以使用系统提供的API函数: 
    int StrCmpLogicalW(
       LPCWSTR psz1,
       LPCWSTR psz2
    );
    Minimum DLL Version       : Shlwapi.dll version 5.5 or later 
    Custom Implementation     : No
    Header                    : Shlwapi.h
    Import library            : Shlwapi.lib
    Minimum operating systems : Windows XP
    Unicode                   : Implemented as Unicode version.

Treatment of the period as a meta-character starts with the Windows Vista build of version 6.00. It is disabled if the
following registry setting, which has programmatic support as the Windows Policy POLID_NoDotBreakInLogicalCompare,
evaluates as true:

Key HKEY_LOCAL_MACHINE/Software/Policies/Microsoft/Windows/Explorer
HKEY_CURRENT_USER/Software/Policies/Microsoft/Windows/Explorer Value NoDotBreakInLogicalCompare Type boolean Default false

The keys are listed in decreasing order of precedence. For interpretation, see Windows Policies.
The StrCmpLogicalW function is exported by name from SHLWAPI versions 6.00 and higher.    

 

此函数的具体使用方法请参考MSDN

 更详细的关于Windows排序规则的讨论请参考以下文章: 
http://blogs.msdn.com/michkap/archive/2005/01/05/346933.aspx

 

 

调用此函数的例子:http://www.pinvoke.net/default.aspx/shlwapi/StrCmpLogicalW.html

C# Sample Code:

  public class StringLogicalComparer: IComparer
  {
    [DllImport("shlwapi.dll", CharSet=CharSet.Unicode, ExactSpelling=true)]
    public static extern int StrCmpLogicalW(string x, string y);

    public int Compare(object x, object y)
    {
      return StrCmpLogicalW((string)x, (string)y);
    }
  }

VB Sample Code:

Private Class StringLogicalComparer
  Implements IComparer(Of String)

  Private Declare Unicode Function StrCmpLogicalW Lib "shlwapi" _
    (ByVal s1 As String, ByVal s2 As String) As Integer

  Public Function Compare(ByVal x As String, ByVal y As String) _
    As Integer _
    Implements System.Collections.Generic.IComparer(Of String).Compare

    Return StrCmpLogicalW(x, y)
  End Function
End Class

Private Sub Test()
  Dim cp As New StringLogicalComparer
  Debug.WriteLine(cp.Compare("a", "a"))    ' 0
  Debug.WriteLine(cp.Compare("1", "1"))    ' 0
  Debug.WriteLine(cp.Compare("a1", "a2"))  ' -1
  Debug.WriteLine(cp.Compare("a10", "a2")) ' +1
End Sub

 

 StrCmpLogicalW排序的例子: 

http://www.codeproject.com/KB/cpp/ListCtrlSort.aspx

原创粉丝点击