Identify that there is a disc in the drive

Sometimes when we double click on a USB drive in Windows File Explorer there is a message "There is no disc in the drive". I want to identify this issue in my application prior to reading any file on the disc.

How is it possible?

I am on Windows Platform and using Visual C++ for development.


If you know the drive letter, you can try the following:

HANDLE h = CreateFile("\.E:", 0, 0, NULL, OPEN_EXISTING, 0, NULL);
if (h == INVALID_HANDLE_VALUE)
{
    DWORD err = GetLastError();
    if (err == ERROR_FILE_NOT_FOUND)
        printf("The drive E: is not readyn");
    else
        printf("Unknown error %lun", (int)err);
}
else
{
    CloseHandle(h); /* don't forget to close the handle! */
    printf("The drive E: is readyn");
}

That is, open the drive without requesting read or write access. It should fail only if the drive is not ready. It works with a USB memory stick.

链接地址: http://www.djcxy.com/p/55948.html

上一篇: 用于镜像的通用图形驱动程序

下一篇: 确定驱动器中有光盘