How can I detect if there is a floppy in a drive?

前端 未结 7 1789
孤独总比滥情好
孤独总比滥情好 2021-01-18 23:03

I tried to use DriveInfo.IsReady, but it returns false if an unformatted floppy is in the drive.

7条回答
  •  北荒
    北荒 (楼主)
    2021-01-18 23:04

    Jonas stuff worked:

    bool MyDll::Class1::HasFloppy( wchar_t driveLetter ) {
    wchar_t path[] = L"\\\\.\\A:";
    path[ 4 ] = driveLetter;
    
    SetLastError( 0 );
    HANDLE drive = CreateFile( path, //__in      LPCTSTR lpFileName,
               GENERIC_READ,     //__in      DWORD dwDesiredAccess,
               0,                //__in      DWORD dwShareMode,
               0,                //__in_opt  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
               OPEN_EXISTING,    //__in      DWORD dwCreationDisposition,
               0,                //__in      DWORD dwFlagsAndAttributes,
               0                 //__in_opt  HANDLE hTemplateFile
    );
    
    DWORD bytes_read;
    char buf[ 512 ];
    DWORD err( 0 );
    if( !ReadFile( drive, buf, 512, &bytes_read, 0 ) )
        err = GetLastError();
    
    CloseHandle( drive );
    return err != ERROR_NOT_READY;
    

    }

提交回复
热议问题