HPFE455
asked on
How to identify user selected is a drive or folder or a short cut
I have a requirement, if the user selected (using mouse or keybroard or right mouse clicking) is a drive or short cut etc.
If the user selected is "C:\" or "D:\" etc (just a drive), the application need to behave in a different way. Similar way for short cut also..
Is there any windows API which identify the user selected type. ( similar to GetDriveType)
If the user selected is "C:\" or "D:\" etc (just a drive), the application need to behave in a different way. Similar way for short cut also..
Is there any windows API which identify the user selected type. ( similar to GetDriveType)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Since a drive is nothing but a folder, I mentoned that one for this purpose. If you want to absolutely on the safe side, you can use 'PathIsRoot()' (http://msdn.microsoft.com/en-us/library/windows/desktop/bb773674%28v=vs.85%29.aspx)
#include <shlwapi.h>
// ...
if (PathIsRoot(szPath)) {
// is a drive
}
ASKER
wchar_t szDir;
bool bFound =false;
if(FILE_ATTRIBUTE_DIRECTOR Y == GetFileAttributes(szDir)) {
bFound = True;
}
Case 1: szDir="C:\"
bFound= False
Case 2:szDir=" "C:"
bFound= True
Case 3:If I pass C:\Temp\Windows\ or C:\Temp\Windows
bFound = false;
bool bFound =false;
if(FILE_ATTRIBUTE_DIRECTOR
bFound = True;
}
Case 1: szDir="C:\"
bFound= False
Case 2:szDir=" "C:"
bFound= True
Case 3:If I pass C:\Temp\Windows\ or C:\Temp\Windows
bFound = false;
You can't use '==', 'GetFileAttributes()' returns a bit mask that contains a multitude of atrributes, it has to be a bitwise AND as in
if (FILE_ATTIBUTE_DIRECTORY & GetFileAttributes(szPath)) {
// is a folder
}
ASKER
I am using VIsual Studio 2010, The above API is not working as expected. I don't know anything wrong with my code:
The above code always hitting m_bExclude = true;//
#include "stdafx.h"
#include <windows.h>
void main()
{
bool m_bExclude = false;
if (FILE_ATTRIBUTE_DIRECTORY & GetFileAttributes(L"C:")) {
m_bExclude = true;// is a folder
}
if (FILE_ATTRIBUTE_DIRECTORY & GetFileAttributes(L"C:\\Windows")) {
m_bExclude = true;// is a folder
}
}
The above code always hitting m_bExclude = true;//
Also for
If that still behaves incorrectly, use
instead.
if (FILE_ATTRIBUTE_DIRECTORY & GetFileAttributes(L"C:\\")) {
m_bExclude = true;// is a folder
}
If that still behaves incorrectly, use
if (PathIsRoot(szPath)) {
// is a drive
}
instead.
ASKER
Thanks jkr, the below solution is working as expected
if (PathIsRoot(szPath)) {
// is a drive
}
if (PathIsRoot(szPath)) {
// is a drive
}
a drive (folder) easily could be checked by length. but, the user could add virtual drives which point to a (local or remote) folder.
Sara