Home > Back-end >  C iterates through all the drive of computer problems
C iterates through all the drive of computer problems

Time:04-18

 
# include "stdafx. H"
#include
#include
#include
#include
#include
# include "atlstr. H"


using namespace std;
Char * ConstCharToChar (const char * CStr)
{
DWORD dwLen=GetLogicalDriveStrings (0, NULL);//get the length of the string system.
Char * c1=new char [dwLen];//build a corresponding length of an array.

DWORD I=0;
For (I=0; I & lt; DwLen; I++)
{
If (CStr [I]=='\ 0')/* check whether source array element is empty, YES, implement the assignment, NO, exit */
break;
The else
C1 [I]=CStr [I];
}
Return c1;
}
Int main ()
{
Char driveType [21]={0};
UINT nType;

DWORD dwLen=GetLogicalDriveStrings (0, NULL);//get the length of the string system.
Char * pszDriver;//=new char [dwLen];//build a corresponding length of an array.
//string pathFormat1;
Const char * lPath;

For (char a='a'; A. & lt;='Z'; +)
{

//pathFormat1=a + ": \ ";
//pszDriver=StrToChar (pathFormat1);

LPath=a + ": \ " + '\ 0';
PszDriver=ConstCharToChar (lPath);

Cstrings STR=cstrings (pszDriver);
LPWSTR wpszDriver=new WCHAR [STR. GetLength () + 1);

//wcscpy (LPTSTR wpszDriver, T2W ((LPTSTR) STR. GetBuffer (NULL)));
Wcscpy_s (LPWSTR wpszDriver, strlen (pszDriver) + 1, T2W ((LPWSTR) STR. GetBuffer (NULL)));
//mbstowcs (wpszDriver pszDriver, strlen (pszDriver) + 1);//Plus null

STR. ReleaseBuffer ();


Cout & lt; <"% c: \ " & lt;
NType=GetDriveType (wpszDriver);

If (nType!=DRIVE_NO_ROOT_DIR)//DRIVE_NO_ROOT_DIR: invalid path
{
The switch (nType)
{
Case DRIVE_FIXED:
Strcpy_s (driveType, "hard");
break;
Case DRIVE_REMOVABLE:
Strcpy_s (driveType, mobile hard disk "");
break;
Case DRIVE_CDROM:
Strcpy_s (driveType, "CD");
break;
Case DRIVE_RAMDISK:
Strcpy_s (driveType, "RAM disk");
break;
Case DRIVE_REMOTE:
Strcpy_s (driveType, "Remote (Network) drive Network disk");
break;
Case DRIVE_UNKNOWN:
Default:
Strcpy_s (driveType, "unknown plate");
break;
}
Cout & lt; }
}

getchar();
return 0;
}


LPath=a + ": \ " + '\ 0'; Numerical wrong at this step, can you tell me what should write to correct iterates through all the computer disk?

CodePudding user response:

Fyi:
 # include "stdafx. H" 
using namespace std;
Struct {
UINT type; LPCSTR a name;
} DriveTypeFlags []={//GetDriveType return code and human-readable string table
{DRIVE_UNKNOWN, "unknown"},
{DRIVE_NO_ROOT_DIR, "invalid path"},
{DRIVE_REMOVABLE, "mobile"},
{DRIVE_FIXED, "fixed"},
{DRIVE_REMOTE, "a network drive"},
{DRIVE_CDROM, "cd-rom"},
{DRIVE_RAMDISK, "random access disk"},
{0, NULL},
};
Int _tmain () {
if (! AfxWinInit (: : GetModuleHandle (NULL), NULL, : : GetCommandLine (), 0)) {
Cerr & lt; <_T (" Fatal Error: MFC initialization failed ") & lt; return -1;
}

//get logical drive string - a: \ & lt; Null> B: \ & lt; Null> C: \ & lt; Null> . Z: \ & lt; Null>
//can also use the GetLogicalDrives instead of a string in the form of a bitmap information
TCHAR buf [200].
DWORD len=GetLogicalDriveStrings (sizeof (buf)/sizeof (TCHAR), buf);

String MSG="logical drive: \ n";
For (TCHAR * s=buf; * s; S +=_tcslen (s) + 1) {
LPCTSTR sDrivePath=s;
MSG +=sDrivePath;
MSG +="";
UINT uDriveType=GetDriveType (sDrivePath);
for (int i=0; DriveTypeFlags [I]. Name; I++) {
If (uDriveType==DriveTypeFlags [I] type) {
MSG +=DriveTypeFlags [I]. Name;
break;//
}
}
MSG +='\ n';
}
Cout & lt; return 0;
}
  • Related