Link to home
Start Free TrialLog in
Avatar of HPFE455
HPFE455Flag for United States of America

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)
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
to add to above:

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
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
}

Open in new window

Avatar of HPFE455

ASKER

wchar_t szDir;

bool bFound =false;
if(FILE_ATTRIBUTE_DIRECTORY == 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;
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
}

Open in new window

Avatar of HPFE455

ASKER

I am using VIsual Studio 2010, The above API is not working as expected.  I don't know anything wrong with my code:
#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
}


}

Open in new window


The above code always hitting  m_bExclude = true;//
Also for

if (FILE_ATTRIBUTE_DIRECTORY & GetFileAttributes(L"C:\\")) {

  m_bExclude = true;// is a folder
}

Open in new window


If that still behaves incorrectly, use

if (PathIsRoot(szPath)) {

  // is a drive
}

Open in new window


instead.
Avatar of HPFE455

ASKER

Thanks jkr,  the below solution is working as expected

if (PathIsRoot(szPath)) {

  // is a drive
}