用過 GetLongPathName API函數(shù)的人都知道,它是一個Platform SDK中用于文件I/O的一個函數(shù),但是它只能在Windows 98 和 Windows 2000 中使用。本文提供一個MFC 全程仿真函數(shù) GetLongPathNameThis,這個函數(shù)可以在所有Win32 OS中使用。
/*
頭文件
Module : LONGPATH.H
Purpose: Interface for a function to emulate GetLongPathName on all Win32 OS's
Created: PJN / 16-06-1999
History:
Copyright (c) 1999 by PJ Naughter.
All rights reserved.
*/
///////////////////////////////// Defines //////////////////////////////////
#ifndef __LONGPATH_H__
#define __LONGPATH_H__
////////////////////////////// Functions ////////////////////////////////////
BOOL GetLongPathName(const CString& sFilename, CString& sLongFilename);
#endif // __LONGPATH_H__
/*
實(shí)現(xiàn)文件
Module : LONGPATH.CPP
Purpose: Implementation for a function to emulate GetLongPathName on all Win32 OS's
Created: PJN / 16-06-1999
History:
Copyright (c) 1999 by PJ Naughter.
All rights reserved.
*/
///////////////////////////////// Includes //////////////////////////////////
#include "stdafx.h"
#include "longpath.h"
///////////////////////////////// Defines ////////////////////////////////////
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
////////////////////////////// Implementation /////////////////////////////////
BOOL GetLongPathName(const CString& sFilename, CString& sLongFilename)
{
//First get the long filename version of the filename
WIN32_FIND_DATA fd;
HANDLE hFind = FindFirstFile(sFilename, &fd);
if (hFind != INVALID_HANDLE_VALUE)
{
FindClose(hFind);
sLongFilename = fd.cFileName;
}
else
return FALSE;
//Now get the long filename version of each part of the path
int nSlash = sFilename.ReverseFind(_T('\\'));
CString sTemp(sFilename);
while (nSlash != -1)
{
sTemp = sTemp.Left(nSlash);
if (sTemp.GetLength() != 2)
{
HANDLE hFind = FindFirstFile(sTemp, &fd);
if (hFind != INVALID_HANDLE_VALUE)
{
FindClose(hFind);
sLongFilename = CString(fd.cFileName) + _T("\\") + sLongFilename;
}
else
{
sLongFilename = sTemp + _T("\\") + sLongFilename;
sTemp.Empty();
}
}
else
{
sLongFilename = sTemp + _T("\\") + sLongFilename;
sTemp.Empty();
}
nSlash = sTemp.ReverseFind(_T('\\'));
}
return TRUE;
}
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報。