Baxtor
Baxtor

Reputation:

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

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

Upvotes: 5

Views: 1413

Answers (7)

Luc Moerman
Luc Moerman

Reputation:

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.

Upvotes: 0

Baxtor
Baxtor

Reputation:

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;

}

Upvotes: 0

Armin Ronacher
Armin Ronacher

Reputation: 32553

Simply speaking: you can't. Floppy drives don't support that.

Upvotes: 1

Jonas Engström
Jonas Engström

Reputation: 5065

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

Upvotes: 3

FlySwat
FlySwat

Reputation: 175643

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

Upvotes: 0

Kevin Fairchild
Kevin Fairchild

Reputation: 10911

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

Upvotes: 0

bhinks
bhinks

Reputation: 1409

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.

Upvotes: 0

Related Questions