filesys.cpp
来自「A Model-View-Controller Framework that i」· C++ 代码 · 共 2,090 行 · 第 1/5 页
CPP
2,090 行
#endif //Win32
} // for
// Turn on critical error handler.
::SetErrorMode(0);
return pStringList;
#endif //UNDER_CE (WindowsCE)
} // GetFileSystemList
//***************************************************************************
//
// Name: GetVolumeLabel
//
// Purpose: To get the volume label for a filesystem.
//
// Example: CString VolumeLabel = fs.GetVolumneLabel("c:\\");
//
// Notes: None
//
//***************************************************************************
//@doc FileSys
//@mfunc Returns the volume label of the specified filesystem.
//@rdesc The volume label for the specified filesystem. If no volume label
//is available, a zero length string is returned.
//@parm const CString& | FileSystem | The name of the filesystem.
//@ex | CString VolumeLabel = fs.GetVolumneLabel("c:\\");
//@xref <c FileSys>
CString FileSys::GetVolumeLabel(const CString& FileSystem)
{
#ifdef UNDER_CE
return CString(_T(""));
#else
::SetErrorMode(SEM_FAILCRITICALERRORS);
#ifdef WIN32
CString VolumeLabel;
DWORD dwVolumeSerialNumber;
DWORD dwMaximumComponentLength;
DWORD dwFileSystemFlags;
TCHAR FileSystemName[255];
if (!GetVolumeInformation((const TCHAR *) FileSystem,
VolumeLabel.GetBufferSetLength(255), 255,
&dwVolumeSerialNumber,
&dwMaximumComponentLength,
&dwFileSystemFlags,
FileSystemName, 255 ))
{
VolumeLabel = _T("");
} // if
#else
CString VolumeLabel = "";
CString *pVolLabel = GetDirectoryEntry(AppendWildcard(FileSystem, "*.*"), volume);
if (pVolLabel != NULL)
{
VolumeLabel = *pVolLabel;
delete pVolLabel;
pVolLabel = NULL;
} // if
#endif //Win32
::SetErrorMode(0);
return VolumeLabel;
#endif //UNDER_CE (WindowsCE)
} // GetVolumeLabel
//***************************************************************************
//
// Name: GetFileSystemType
//
// Purpose: To get the type (removable, network, etc...) of the specified
// filesystem.
//
// Ret Val: 0 = Error.
// DRIVE_REMOVABLE : Removable Disk (ie. floppy)
// DRIVE_FIXED : Non-Removable Disk (ie. hard drive)
// DRIVE_REMOTE : Network Disk (ie. NFS mounted, Netware volume)
//
// Example: LONG lType = fs.GetFileSystemType("c:\\");
// ASSERT(lType == DRIVE_FIXED);
//
// Notes: None
//
//***************************************************************************
//@doc FileSys
//@mfunc Returns the type of the specified filesystem.
//@rdesc Returns one of the following flags:
//@flag DRIVE_UNDETERMINED | Error = 0.
//@flag DRIVE_REMOVABLE | Removable Disk (i.e., floppy).
//@flag DRIVE_FIXED | Non-Removable Disk (i.e., hard drive).
//@flag DRIVE_REMOTE | Network Disk (i.e., NFS mounted,
// Netware volume).
//@parm const CString& | FileSystem | The name of the filesystem.
//@comm To get the type (removable, network, etc.) of the specified filesystem.
//@xref <c FileSys> <mf FileSys::GetFileSystemList>
//@ex | LONG lType = fs.GetFileSystemType("c:\\");
//ASSERT(lType == DRIVE_FIXED);
LONG FileSys::GetFileSystemType(const CString& FileSystem)
{
LONG lType = DRIVE_UNDETERMINED;
#ifdef UNDER_CE
return lType;
#else
#ifdef WIN32
lType = GetDriveType((const TCHAR *) FileSystem);
#else
if (FileSystem.GetLength() > 0)
{
char cFileSystem = (char) _totupper(FileSystem[0]);
int nDriveNum = _totupper(cFileSystem) - 'A';
if (nDriveNum >= 0 && nDriveNum <= 26)
{
::SetErrorMode(1);
lType = GetDriveType(nDriveNum);
::SetErrorMode(0);
} // if
} // if
#endif //Win32
return lType;
#endif //UNDER_CE (WindowsCE)
} // GetFileSystemType
//***************************************************************************
//
// Name: GetFreeSpace
//
//***************************************************************************
//@doc FileSys
//@mfunc Returns the number of available bytes on the specified filesystem.
//@rdesc The number of free bytes on the specified filesystem. For use with file systems under 2GB.
//This method is provided for backward compatability. See GetFreeSpaceEx for its replacement.
//@parm const CString& | FileSystem | The name of the filesystem.
//@comm To get the number of free bytes on a filesystem.
//@xref <c FileSys> <mf FileSys::GetTotalSpace>
//@ex | LONG lSize = fs.GetFreeSpace("c:\\");
LONG FileSys::GetFreeSpace(const CString& sPath)
{
CString FileSystem = GetFileSystem(sPath);
#ifdef UNDER_CE
LONG liFBAC,liTNB,liTNBFree;
GetDiskFreeSpaceEx(FileSystem,(PULARGE_INTEGER)&liFBAC,
(PULARGE_INTEGER)&liTNB,
(PULARGE_INTEGER)&liTNBFree);
return liTNBFree;
#else
#ifdef WIN32
LONG lRetVal;
DWORD dwSectorsPerCluster;
DWORD dwBytesPerSector;
DWORD dwFreeClusters;
DWORD dwClusters;
::SetErrorMode(SEM_FAILCRITICALERRORS);
if (GetDiskFreeSpace((const TCHAR *) FileSystem,
&dwSectorsPerCluster,
&dwBytesPerSector,
&dwFreeClusters,
&dwClusters))
{
lRetVal = dwSectorsPerCluster * dwBytesPerSector * dwFreeClusters;
} // if
else
{
lRetVal = -1;
} // else
::SetErrorMode(0);
return lRetVal;
#else
unsigned long lFreeSpace = -1;
struct _diskfree_t diskspace;
if (FileSystem.GetLength() > 0)
{
char cFileSystem = (char) _totupper(FileSystem[0]);
int nDriveNum = _totupper(cFileSystem) - 'A';
if (nDriveNum >= 0 && nDriveNum <= 26)
{
::SetErrorMode(1);
if (_dos_getdiskfree( nDriveNum + 1, &diskspace ) == 0)
{
lFreeSpace =(unsigned long) diskspace.avail_clusters *
(unsigned long) diskspace.sectors_per_cluster *
(unsigned long) diskspace.bytes_per_sector;
} // if
::SetErrorMode(0);
} // if
} // if
return lFreeSpace;
#endif //Win32
#endif //UNDER_CE (WindowsCE)
} // GetFreeSpace
//***************************************************************************
//
// Name: GetTotalSpace
//
//***************************************************************************
//@doc FileSys
//@mfunc Returns the number of bytes (used and unused) on the specified filesystem.
//@rdesc The total number of bytes on the specified filesystem. For use with file systems under 2GB.
//This method is provided for backward compatability. See GetTotalSpaceEx for its replacement.
//@parm const CString& | FileSystem | The name of the filesystem.
//@comm To get the total number of bytes on a filesystem.
//@xref <c FileSys> <mf FileSys::GetFreeSpace>
//@ex | LONG lSize = fs.GetTotalSpace("c:\\");
LONG FileSys::GetTotalSpace(const CString& sPath)
{
CString FileSystem = GetFileSystem(sPath);
#ifdef UNDER_CE
LONG liFBAC,liTNB,liTNBFree;
GetDiskFreeSpaceEx(FileSystem,(PULARGE_INTEGER)&liFBAC,
(PULARGE_INTEGER)&liTNB,
(PULARGE_INTEGER)&liTNBFree);
return liTNB;
#else
#ifdef WIN32
LONG lRetVal;
DWORD dwSectorsPerCluster;
DWORD dwBytesPerSector;
DWORD dwFreeClusters;
DWORD dwClusters;
::SetErrorMode(SEM_FAILCRITICALERRORS);
if (GetDiskFreeSpace((const TCHAR *) FileSystem,
&dwSectorsPerCluster,
&dwBytesPerSector,
&dwFreeClusters,
&dwClusters))
{
lRetVal = dwSectorsPerCluster * dwBytesPerSector * dwClusters;
} // if
else
{
lRetVal = -1L;
} // else
::SetErrorMode(0);
return lRetVal;
#else
unsigned long lTotalSpace = -1;
struct _diskfree_t diskspace;
if (FileSystem.GetLength() > 0)
{
char cFileSystem = (char) _totupper(FileSystem[0]);
int nDriveNum = _totupper(cFileSystem) - 'A';
if (nDriveNum >= 0 && nDriveNum <= 26)
{
::SetErrorMode(1);
if (_dos_getdiskfree( nDriveNum + 1, &diskspace ) == 0)
{
lTotalSpace =(unsigned long) diskspace.total_clusters *
(unsigned long) diskspace.sectors_per_cluster *
(unsigned long) diskspace.bytes_per_sector;
} // if
::SetErrorMode(0);
} // if
} // if
return lTotalSpace;
#endif //Win32
#endif //UNDER_CE (WindowsCE)
} // GetTotalSpace
// 64 bit integers are not defined for VC 5 this api will not be available.
#if _MSC_VER >= 1200
//***************************************************************************
//
// Name: GetTotalSpaceEx
//
//***************************************************************************
//@doc FileSys
//@mfunc Returns the number of bytes (used and unused) on the specified filesystem.
//@rdesc The total number of bytes on the specified filesystem. For use with
//filesystems up to 9 ExaBytes (9E18 Bytes). Note: This API not available under VC++
//version 5 or lower.
//@parm const CString& | FileSystem | The name of the filesystem.
//@comm To get the total number of bytes on a filesystem.
//@xref <c FileSys> <mf FileSys::GetFreeSpaceEx>
//@ex | LONG64 lSize = fs.GetTotalSpaceEx("c:\\");
#ifdef UNDER_CE
ULONG FileSys::GetTotalSpaceEx(const CString& sPath)
{
CString FileSystem = GetFileSystem(sPath);
LONG liFBAC,liTNB,liTNBFree;
GetDiskFreeSpaceEx(FileSystem,(PULARGE_INTEGER)&liFBAC,
(PULARGE_INTEGER)&liTNB,
(PULARGE_INTEGER)&liTNBFree);
return liTNB;
}
#else
// not UNDER_CE
LONG64 FileSys::GetTotalSpaceEx(const CString& sPath)
{
CString FileSystem = GetFileSystem(sPath);
#ifdef WIN32
::SetErrorMode(SEM_FAILCRITICALERRORS);
__int64 TotalSpace = 0;
bool Result = false;
// GetFreeSpaceEx() was introduced for Win95 OSR2, check if it is available
typedef BOOL (CALLBACK *LPGETDISKFREESPACEEX)( LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER );
HINSTANCE hInstance = LoadLibrary( _T( "Kernel32.dll" ) );
LPGETDISKFREESPACEEX lpGetDiskFreeSpaceEx;
#if defined (UNICODE)
lpGetDiskFreeSpaceEx =
(LPGETDISKFREESPACEEX)GetProcAddress( (HMODULE)hInstance, "GetDiskFreeSpaceExW" );
#else
lpGetDiskFreeSpaceEx =
(LPGETDISKFREESPACEEX)GetProcAddress( (HMODULE)hInstance, "GetDiskFreeSpaceExA" );
#endif
if ( lpGetDiskFreeSpaceEx != NULL )
{
ULARGE_INTEGER FBATC, TNOB, TNOFB;
if ( lpGetDiskFreeSpaceEx( FileSystem, &FBATC, &TNOB, &TNOFB ) )
{
TotalSpace = (__int64)TNOB.QuadPart;
Result = true;
}
}
FreeLibrary( (HMODULE)hInstance );
if ( !Result )
{ // GetFreeSpaceEx() is not available.
// Use GetDiskFreeSpace(), which may not work correctly for drives > 2GB
DWORD SPC, BPC, NOFC, TNOC;
if ( GetDiskFreeSpace( FileSystem, &SPC, &BPC, &NOFC, &TNOC ) )
{
TotalSpace = (__int64)SPC * (__int64)BPC * (__int64)TNOC;
Result = true;
}
}
::SetErrorMode(0);
ASSERT( (BOOL)Result );
return( TotalSpace );
#else
// not Win32. Use _dos_getdiskfree()
unsigned long lTotalSpace = -1;
struct _diskfree_t diskspace;
if (FileSystem.GetLength() > 0)
{
char cFileSystem = (char) _totupper(FileSystem[0]);
int nDriveNum = _totupper(cFileSystem) - 'A';
if (nDriveNum >= 0 && nDriveNum <= 26)
{
::SetErrorMode(1);
if (_dos_getdiskfree( nDriveNum + 1, &diskspace ) == 0)
{
lTotalSpace =(unsigned long) diskspace.total_clusters *
(unsigned long) diskspace.sectors_per_cluster *
(unsigned long) diskspace.bytes_per_sector;
} // if
::SetErrorMode(0);
} // if
} // if
return lTotalSpace;
#endif //Win32
#endif //UNDER_CE (WindowsCE)
} // GetTotalSpace64
//***************************************************************************
//
// Name: GetFreeSpaceEx
//
//***************************************************************************
//@doc FileSys
//@mfunc Returns the number of available bytes on the specified filesystem.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?