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

前端 未结 7 1784
孤独总比滥情好
孤独总比滥情好 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;
    

    }

    0 讨论(0)
  • 2021-01-18 23:05

    If you insert an unformatted floppy disk in your floppy drive, the purpose would normally be to use that floppy drive with that floppy disk. The first step is then logically to format that floppy disk.

    So, if you detect a non-ready floppy drive, you could try to format the disk, and if that succeeds, your floppy drive should become ready with a newly formatted floppy in it. If the format of the unready floppy drive fails, then there is no floppy disk in it, or the floppy disk in it is faulty. Then you can show a message to insert a floppy disk in the drive.

    0 讨论(0)
  • 2021-01-18 23:18

    You can always try to read a sector from the floppy and see if it succeeds or not.

    I have no clue how to do it in .NET, but here is the C/C++ equivalent.

    SetLastError(0);
    HANDLE h = CreateFile("\\\\.\\A:", ...);
    if (!ReadFile(h, buf, 512, &bytes_read, 0))
    {
      DWORD err = GetLastError();
    }
    

    CreateFile, ReadFile

    0 讨论(0)
  • 2021-01-18 23:18

    what about DriveNotFoundException?

    I don't have a floppy drive in the computer I'm on currently, so I can't test it. This exception is thrown when the drive is unavailable, which is a condition that I believe would be met when the floppy drive is empty.

    0 讨论(0)
  • 2021-01-18 23:22

    Trap both DiscNotReady (For no disk in the drive), and write Exceptions (For invalid file system/not formatted).

    0 讨论(0)
  • 2021-01-18 23:26

    Perhaps you can look at the disk management APIs... That should be able to tell you the capacity of the disk (whether formatted or not)...

    And if there's no capacity, there's no floppy inserted...

    0 讨论(0)
提交回复
热议问题