tags:

views:

428

answers:

7

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

+1  A: 

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

Armin Ronacher
Floppy drives DO support media discovery, Try saving from Notepad to an empty drive or to a drive with an unformatted disk in it. Notepad gives two different responses.
Notice that before it fails, the disk access light comes on. Notepad tries to read a sector on the disk, and if it fails it displays the "please insert a disk" message.
OwenP
A: 

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.

bhinks
So far I can not find any .Net method that acts differently for the no floppy case and the unformatted floppy case
A: 

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

Kevin Fairchild
A: 

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

FlySwat
+3  A: 

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

Jonas Gulle
A: 

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;

}

Sweet :) Happy hacking!
Jonas Gulle
Very nice but it doesn't really look like .NET as you asked..
Andrei Rinea
A: 

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.