📄 fileinfo.hpp
字号:
}
catch(...)
{
}
return CString(_T(""));
}
#endif
inline const CString get_file_info_filename(const CString &cstrFile)
// Return clean filename - like "my_file.txt".
#ifdef _UNICODE
{
try
{
const _bstr_t bstrFile(cstrFile);
_bstr_t bstrDrive(bstrFile);
_bstr_t bstrDirectory(bstrFile);
_bstr_t bstrTitle(bstrFile);
_bstr_t bstrExtension(bstrFile);
::_wsplitpath(
(const wchar_t*)bstrFile,
(wchar_t*)bstrDrive,
(wchar_t*)bstrDirectory,
(wchar_t*)bstrTitle,
(wchar_t*)bstrExtension);
CString cstrFilename(_T(""));
cstrFilename.Format(TEXT("%s%s"), (const char*)bstrTitle, (const char*)bstrExtension);
return cstrFilename;
}
catch(...)
{
}
return CString(_T(""));
}
#else
{
try
{
_bstr_t bstrDrive((LPCTSTR)cstrFile);
_bstr_t bstrDirectory((LPCTSTR)cstrFile);
_bstr_t bstrTitle((LPCTSTR)cstrFile);
_bstr_t bstrExtension((LPCTSTR)cstrFile);
::_splitpath(
(LPCTSTR)cstrFile,
(char*)bstrDrive,
(char*)bstrDirectory,
(char*)bstrTitle,
(char*)bstrExtension);
CString cstrFilename(_T(""));
cstrFilename.Format(TEXT("%s%s"), (const char*)bstrTitle, (const char*)bstrExtension);
return cstrFilename;
}
catch(...)
{
}
return CString(_T(""));
}
#endif
inline const CString get_file_info_filetitle(const CString &cstrFile)
// Return clean filename - like "my_file".
#ifdef _UNICODE
{
try
{
const _bstr_t bstrFile(cstrFile);
_bstr_t bstrDrive(bstrFile);
_bstr_t bstrDirectory(bstrFile);
_bstr_t bstrTitle(bstrFile);
_bstr_t bstrExtension(bstrFile);
::_wsplitpath(
(const wchar_t*)bstrFile,
(wchar_t*)bstrDrive,
(wchar_t*)bstrDirectory,
(wchar_t*)bstrTitle,
(wchar_t*)bstrExtension);
const CString cstrFiletitle((const char*)bstrTitle);
return cstrFiletitle;
}
catch(...)
{
}
return CString(_T(""));
}
#else
{
try
{
_bstr_t bstrDrive((LPCTSTR)cstrFile);
_bstr_t bstrDirectory((LPCTSTR)cstrFile);
_bstr_t bstrTitle((LPCTSTR)cstrFile);
_bstr_t bstrExtension((LPCTSTR)cstrFile);
::_splitpath(
(LPCTSTR)cstrFile,
(char*)bstrDrive,
(char*)bstrDirectory,
(char*)bstrTitle,
(char*)bstrExtension);
const CString cstrFiletitle((const char*)bstrTitle);
return cstrFiletitle;
}
catch(...)
{
}
return CString(_T(""));
}
#endif
inline const CString get_file_info_location(const CString &cstrFile)
// Get the location (or directory) where to find this object - like "c:\temp".
{
CString cstrLocation(_T(""));
cstrLocation.Format(TEXT("%s:%s"), (LPCTSTR)get_file_info_drive(cstrFile), (LPCTSTR)get_file_info_directory(cstrFile));
return cstrLocation;
}
inline const CString get_startup_dir()
// Return startup directory of current logged on user.
{
CString cstrPath_StartupDir(_T(""));
cstrPath_StartupDir.Format(TEXT("c:\\documents and settings\\%s\\start menu\\programs\\startup"), (LPCTSTR)get_user());
return cstrPath_StartupDir;
}
inline const CString get_user(const CString &cstrNameInfo)
// Get current user (that is the user logged on). Also return the username.
{
CONST DWORD dwLength = 1024;
TCHAR lpName[dwLength] = _T("");
::GetUserName(lpName, (const_cast<DWORD*>(&dwLength)));
CString cstrNameInfoEx(_T(""));
cstrNameInfoEx.Format(TEXT("%s"), lpName);
return cstrNameInfoEx;
}
inline const bool get_vol(const CString &cstrDriveLetter, CString &cstrVolumename)
// Get name of volume for this drive (if any). Return T on succes and F if not.
{
bool bSucces = false;
DWORD dwVolumeSerialNumber = 0,
dwMaximumComponentLength = 0,
dwFileSystemFlags = 0;
TCHAR pszRoot[MAX_PATH] = _T("?:\\");
TCHAR pszVolumename[MAX_PATH];
TCHAR pszFileSystem[MAX_PATH] = _T("");
cstrVolumename.Empty();
*pszRoot = (TCHAR)::tolower((int)cstrDriveLetter[0]);
bSucces = (::GetVolumeInformation(pszRoot,
pszVolumename,
sizeof(pszVolumename),
&dwVolumeSerialNumber,
&dwMaximumComponentLength,
&dwFileSystemFlags,
pszFileSystem,
sizeof(pszFileSystem)) != 0);
if(bSucces)
{
cstrVolumename = CString(pszVolumename);
cstrVolumename.MakeLower();
}
return bSucces;
}
inline const bool is_directory(const CString &cstrDirectory)
// Return T in case this directory exist and F if not.
{
HANDLE hFind;
WIN32_FIND_DATA fd;
hFind = ::FindFirstFile((LPCTSTR)cstrDirectory, &fd);
::FindClose(hFind);
if(hFind == INVALID_HANDLE_VALUE)
return false;
return((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
}
inline const bool is_file(const CString &cstrFile)
// Return T in case this file exist and F if not.
{ return !is_directory(cstrFile); }
inline const bool is_file_dots(const CString &cstrFile)
// Return T in case this file has the name "." or "..", indicating that is actually a directory, and F if not.
{ return (cstrFile == _T(".") || cstrFile == _T("..")); }
inline const HRESULT shortcut_create(
const CString &cstrLinkObject,
const CString &cstrLinkLocation,
const CString &cstrWorkingDirectory,
const CString &cstrLinkName,
const CString &cstrLinkDescription)
// Create shortcut to specific file at specific path-location. Return HRESULT for op.
{
HRESULT hResult = ::CoInitialize(NULL);
if(SUCCEEDED(hResult))
{
IShellLink* pShellLink = NULL;
::CoCreateInstance(CLSID_ShellLink,
NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink,
(LPVOID*)&pShellLink); // Get Shell link interface ptr.
if(SUCCEEDED(hResult))
{
hResult = pShellLink->SetPath((LPCTSTR)cstrLinkObject);
hResult = pShellLink->SetWorkingDirectory((LPCTSTR)cstrWorkingDirectory);
hResult = pShellLink->SetDescription((LPCTSTR)cstrLinkDescription);
// Ask for persist file int. ptr.
IPersistFile *pPersistFile = NULL;
hResult = pShellLink->QueryInterface(IID_IPersistFile, (void**)&pPersistFile);
if(SUCCEEDED(hResult))
{
WCHAR wszPath_Shortcut[MAX_PATH];
const CString cstrPath_Shortcut(cstrLinkLocation + _T("\\") + cstrLinkName + (cstrLinkLocation.Right(4) == _T(".lnk") ? _T("") : _T(".lnk")));
A2W((LPCTSTR)cstrPath_Shortcut, wszPath_Shortcut, MAX_PATH);
hResult = pPersistFile->Save(wszPath_Shortcut, TRUE);
pPersistFile->Release();
}
pShellLink->Release();
}
::CoUninitialize();
}
return hResult;
}
inline const HRESULT shortcut_delete(
const CString &cstrLinkLocation,
const CString &cstrLinkName,
const bool bExactMatch)
// Delete all (0 or more) link objects that is found on this location. Return HRESULT for op.
{
if(file_or_directory_exist(cstrLinkLocation))
{
WIN32_FIND_DATA fd;
HANDLE hFile = 0;
int iLength = 0;
CString cstrFile_A(_T("")),
cstrFile_B(_T("")),
cstrLinkLocationEx(_T(""));
cstrLinkLocationEx.Format(TEXT("%s\\*.*"), (LPCTSTR)cstrLinkLocation);
hFile = ::FindFirstFile((LPCTSTR)cstrLinkLocationEx, &fd);
while(hFile != INVALID_HANDLE_VALUE)
{
const CString cstrFileName(fd.cFileName);
if(!cstrFileName.IsEmpty() && !is_file_dots(cstrFileName))
{
if(bExactMatch && cstrLinkName.CompareNoCase((LPCTSTR)cstrFileName) == 0)
// Delete only on exact match.
{
CString cstrFileName_FullPath(_T(""));
cstrFileName_FullPath.Format(TEXT("%s\\%s"), (LPCTSTR)cstrLinkLocation, (LPCTSTR)cstrFileName);
::DeleteFile((LPCTSTR)cstrFileName_FullPath);
}
else
{
iLength = min(cstrLinkName.GetLength(), cstrFileName.GetLength());
cstrFile_A = cstrLinkName.Left(iLength);
cstrFile_B = cstrFileName.Left(iLength);
if(cstrFile_A.CompareNoCase((LPCTSTR)cstrFile_B) == 0)
// Delete on beginning-matching names.
{
CString cstrFileName_FullPath(_T(""));
cstrFileName_FullPath.Format(TEXT("%s\\%s"), (LPCTSTR)cstrLinkLocation, (LPCTSTR)cstrFileName);
::DeleteFile((LPCTSTR)cstrFileName_FullPath);
}
}
}
::FindNextFile(hFile, &fd);
if(CString(fd.cFileName) == cstrFileName)
// Has reached the last file.
hFile = INVALID_HANDLE_VALUE;
}
::FindClose(hFile);
}
return S_OK;
}
inline const HRESULT shortcut_edit(
CONST HWND hWnd,
const CString &cstrLinkObject,
const CString &cstrLinkLocation,
const CString &cstrWorkingDirectory,
const CString &cstrLinkName,
const CString &cstrLinkDescription)
// Edit shortcut to specific file at specific path-location. Return HRESULT for op.
{
HRESULT hResult = ::CoInitialize(NULL);
if(SUCCEEDED(hResult))
{
IShellLink* pShellLink = NULL;
::CoCreateInstance(CLSID_ShellLink,
NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink,
(LPVOID*)&pShellLink); // Get Shell link int. ptr.
if(SUCCEEDED(hResult))
{
TCHAR pszPath[MAX_PATH] = "";
TCHAR pszDescription[MAX_PATH] = "";
WIN32_FIND_DATA fd;
IPersistFile* pPersistFile = NULL;
// Ask for persist file int. ptr.
hResult = pShellLink->QueryInterface(IID_IPersistFile, (void**)&pPersistFile);
if(SUCCEEDED(hResult))
{
WCHAR wszPath_Shortcut[MAX_PATH];
const CString cstrPath_Shortcut(cstrLinkLocation + _T("\\") + cstrLinkName + (cstrLinkLocation.Right(4) == _T(".lnk") ? _T("") : _T(".lnk")));
// Load the shortcut.
hResult = pPersistFile->Load(wszPath_Shortcut, STGM_READ);
if(SUCCEEDED(hResult))
{
// Get the path to the link target.
hResult = pShellLink->GetPath(pszPath, MAX_PATH, (WIN32_FIND_DATA*)&fd, SLGP_SHORTPATH);
if(SUCCEEDED(hResult))
{
// Get the description of the target.
hResult = pShellLink->GetDescription(pszPath, MAX_PATH);
if(SUCCEEDED(hResult))
{
// Edit it.
hResult = pShellLink->SetPath((LPCTSTR)cstrLinkObject);
hResult = pShellLink->SetWorkingDirectory((LPCTSTR)cstrWorkingDirectory);
hResult = pShellLink->SetDescription((LPCTSTR)cstrLinkDescription);
hResult = pPersistFile->Save(wszPath_Shortcut, TRUE);
}
}
}
pPersistFile->Release();
}
pShellLink->Release();
}
::CoUninitialize();
}
return hResult;
}
inline const HRESULT shortcut_exist(
CONST HWND hWnd,
const CString &cstrLinkObject,
const CString &cstrLinkLocation,
const CString &cstrWorkingDirectory,
const CString &cstrLinkName,
const CString &cstrLinkDescription)
// Return S_OK if this shortcut exist and S_FALSE if not or no error.
{
HRESULT hResult = ::CoInitialize(NULL);
if(SUCCEEDED(hResult))
{
IShellLink* pShellLink = NULL;
::CoCreateInstance(CLSID_ShellLink,
NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink,
(LPVOID*)&pShellLink); // Get Shell link int. ptr.
if(SUCCEEDED(hResult))
{
TCHAR pszPath[MAX_PATH] = "";
TCHAR pszDescription[MAX_PATH] = "";
IPersistFile* pPersistFile = NULL;
// Ask for persist file int. ptr.
hResult = pShellLink->QueryInterface(IID_IPersistFile, (void**)&pPersistFile);
if(SUCCEEDED(hResult))
{
WCHAR wszPath_Shortcut[MAX_PATH];
const CString cstrPath_Shortcut(cstrLinkLocation + _T("\\") + cstrLinkName + (cstrLinkLocation.Right(4) == _T(".lnk") ? _T("") : _T(".lnk")));
// Load the shortcut.
hResult = pPersistFile->Load(wszPath_Shortcut, STGM_READ);
pPersistFile->Release();
}
pShellLink->Release();
}
::CoUninitialize();
}
return hResult;
}
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - -
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// 0. Data. (alphabetical).
// 1. Object. (alphabetical).
// 2. Event's. (alphabetical).
// 3.0. Menu choice. (menu order).
// 3.1. Menu choice, enablers. (menu order).
// 4. Slaves. (alphabetical).
// 5. Other. (alphabetical).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -