English 中文(简体)
确定两个名录名称是否指同一名录
原标题:Determine if two directory names point to the same directory

我有两条载有双管线的道路。 是否有办法确定它们是否指明同一地点? 进行严格比较似乎不可靠,因为诸如案件敏感性、8.3份档案名称长度、标题、次等。

为了说明这一点,我如何能够确定这两个点的位置是:

String1 = "c:Program FilesMyFolder"
String2 = "C:PROGRA~1MYFOLDER"
最佳回答

下述法典应当适用于档案(包括硬链接)和目录(包括指令),但这两种途径必须有效!

#include <windows.h>
#include <stdio.h>

BOOL ArePathsEqual(LPCTSTR path1,LPCTSTR path2) 
{
    BY_HANDLE_FILE_INFORMATION bhfi1,bhfi2;
    HANDLE h1, h2 = NULL;
    DWORD access = 0;
    DWORD share = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;

    h1 = CreateFile(path1,access,share,NULL,OPEN_EXISTING,(GetFileAttributes(path1)&FILE_ATTRIBUTE_DIRECTORY)?FILE_FLAG_BACKUP_SEMANTICS:0,NULL);
    if (INVALID_HANDLE_VALUE != h1) 
    {
        if (!GetFileInformationByHandle(h1,&bhfi1)) bhfi1.dwVolumeSerialNumber = 0;
        h2 = CreateFile(path2,access,share,NULL,OPEN_EXISTING,(GetFileAttributes(path2)&FILE_ATTRIBUTE_DIRECTORY)?FILE_FLAG_BACKUP_SEMANTICS:0,NULL);
        if (!GetFileInformationByHandle(h2,&bhfi2)) bhfi2.dwVolumeSerialNumber = bhfi1.dwVolumeSerialNumber + 1;
    }
    CloseHandle(h1);
    CloseHandle(h2);
    return INVALID_HANDLE_VALUE != h1 && INVALID_HANDLE_VALUE != h2 
    && bhfi1.dwVolumeSerialNumber==bhfi2.dwVolumeSerialNumber
    && bhfi1.nFileIndexHigh==bhfi2.nFileIndexHigh
    && bhfi1.nFileIndexLow==bhfi2.nFileIndexLow ;
}

void main() 
{
    BOOL bRet = ArePathsEqual("c:\program files","c:\progra~1");
    printf("ArePathsEqual: %d
",bRet);
}
问题回答

为了取得最佳业绩,你应减少通俗形式的道路。 这就是8.3(GetShortPathName)和下级。

这是C#样本。 如果你使用其他某种语言的话,这个想法就是一样的。 利用GetLongPathName和(或)GetShortPathName,从Ckernel32.dll中比较:

 [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
 public static extern int GetLongPathName(
    [MarshalAs(UnmanagedType.LPTStr)]
    string path,
    [MarshalAs(UnmanagedType.LPTStr)]
    StringBuilder longPath,
    int longPathLength
    );

 [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
 public static extern int GetShortPathName(
    [MarshalAs(UnmanagedType.LPTStr)]
    string path,
    [MarshalAs(UnmanagedType.LPTStr)]
    StringBuilder shortPath,
    int shortPathLength
    );


function bool PathsAreEqual(string path1, string path2)
{
    StringBuilder shortPath1 = new StringBuilder(255);
    StringBuilder shortPath2 = new StringBuilder(255);
    GetShortPathName(path1, shortPath1, shortPath1.Capacity);
    GetShortPathName(path2, shortPath2, shortPath2.Capacity);

    return shortPath1.ToString().ToLower() == shortPath2.ToString().ToLower();

}




相关问题
How to read exact number of bytes from a stream (tcp) socket?

In winsock, both the sync recv and the async WSARecv complete as soon as there is data available in a stream socket, regardless of the size specified (which is only the upper limit). This means that ...

AcquireCredentialsHandle returns SEC_E_NO_CREDENTIALS

I created a self-signed certificate (created using OpenSSL) and installed it into the Certificate Store using the Certificates MMC snap-in (CertMgr.msc) on Windows Vista Ultimate. I have managed to ...

Calling Win32 EnumThreadWindows() in C#

I m trying to get a call to EnumThreadWindows working, but I always get a Wrong Parameter-Error, although my code is nearly the same as this example on pinvoke.net. I don t know why this doesn t work: ...

COM Basic links

folks can you provide me the tutorial link or .pdf for learning basic COM?. i do google it.. still i recommend answers of stackoverflow so please pass me.. Thanks

Handling multiple windows WIN32 API

HI I m trying to create an application in the Win32 environment containing more than one window. How do i do that? all the Win32 Tutorials on web i found only showed how to manage one window. How do i ...

Creating a thread in DllMain?

It seems that when a thread is created from within DllMain upon DLL_PROCESS_ATTACH it won t begin until all dll s have been loaded. Since I need to make sure the thread runs before I continue, I get a ...

热门标签