简体   繁体   English

我正在使用QDir()。isReadable来检查驱动器是否可读。 在Qt Creator中它运行正常,但是当我运行exe时它会一直给我错误

[英]I'm using QDir().isReadable to check if a drive is readable. In the Qt Creator it runs fine, but when I run the exe it keeps giving me errors

I'm using it like this: 我这样使用它:

if(QDir("G:/").isReadable()){
    qDebug("G Is readable!"); //Do something with G:/
}

As I said, in Qt Creator it runs fine without a problem, it checks if the drive is readable, and if so it prints it to the console, if not, it does nothing. 正如我所说,在Qt Creator中它运行良好而没有问题,它检查驱动器是否可读,如果是,它将其打印到控制台,如果没有,它什么都不做。

But when I run the .exe file, it keeps giving me errors each time it does the check (every 2 seconds) if the drive isn't readable. 但是当我运行.exe文件时,如果驱动器不可读,它每次检查时都会给我错误(每2秒)。

"There is no disk in the drive. Please insert a disk into drive G:." “驱动器中没有磁盘。请将磁盘插入驱动器G:。”

I don't want this error to keep appearing, what do I do? 我不希望这个错误继续出现,我该怎么办?

Edit: I think it's the isReadable function that causes the problem, is there any other way to do what I want to do? 编辑:我认为这是导致问题的isReadable函数,有没有其他方法可以做我想做的事情? Or maybe should I write the code myself? 或者我应该自己编写代码?

This message is generated by Windows. 此消息由Windows生成。

There is a workaround for users that have applications that cannot be fixed. 对于具有无法修复的应用程序的用户,有一种解决方法。 The error messages may be suppressed by setting 2 to registry key ErrorMode in: 通过将注册表项ErrorMode设置为2 ,可以抑制错误消息:

Computer\HKEY_LOCAL\MACHINE\SYSTEM\CurrentControlSet\Control\Windows

It looks that if QDir::isReadable() is called after removing the media it triggers that error. 看起来,如果在删除媒体后调用QDir::isReadable() ,则会触发该错误。 QDir::exists() always returns true if the drive letter is present in the system, so it cannot be used here. 如果系统中存在驱动器号, QDir::exists()始终返回true ,因此不能在此处使用它。

For now I see that it is possible to check removable media using native Windows API, see the answer to How to detect if media is inserted into a removable drive/card reader 现在我看到可以使用本机Windows API检查可移动媒体,请参阅如何检测媒体是否插入可移动驱动器/读卡器的答案

The following code is able to detect that the media is removed without triggering the error: 以下代码能够检测到媒体被删除而不会触发错误:

#include <windows.h>

HANDLE hDevice = CreateFile (L"\\\\.\\G:",         // like "\\.\G:"
                             FILE_READ_ATTRIBUTES, // read access to the attributes
                             FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
                             NULL, OPEN_EXISTING, 0, NULL);

if (hDevice == INVALID_HANDLE_VALUE) {
    // not valid device
    return;
}

WORD cbBytesReturned;
bool bSuccess = DeviceIoControl (hDevice,                // device to be queried
                            IOCTL_STORAGE_CHECK_VERIFY2,
                            NULL, 0,                     // no input buffer
                            NULL, 0,                     // no output buffer
                            (LPDWORD)&cbBytesReturned,   // # bytes returned
                            NULL);                       // synchronous I/O

CloseHandle(hDevice); // close handle

if (bSuccess && QDir("G:/").isReadable()) {
    // G is readable
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的 .exe 在 Qt Creator 中运行后立即崩溃 - My .exe crashes as soon as I run it in Qt Creator 使用Qt创建器时出现C2440错误,但使用VS2010 Express可以正常运行代码 - C2440 error when using Qt creator but code runs fine using VS2010 Express 我尝试在linuxLite上使用qt creator运行qt-vtk-project - I try to run qt-vtk-project with qt creator on linuxLite Wininet给出了链接器错误,我正在使用devc ++ - Wininet giving linker errors I'm using devc++ 我使用Eclipse Luna,并且正在尝试C ++,当我看到人们做得很好时,为什么它会给我错误? - I use Eclipse Luna and I'm trying out C++, why does it give me errors when I see people doing it fine? Qt Creator + OpenCV:程序从.exe运行,但不从编辑器运行 - Qt Creator + OpenCV: Program runs from .exe but not from editor 我的程序运行良好,我可以复制对象,但是当我使用复制分配 (=) 时,它仍然运行良好。 为什么不报错? - My program runs fine and I am able copy the object however when I used the copy assignment (=) it still runs fine. Why is it not giving error? 为什么我的代码在运行时给我一个stackdump错误? - Why my code giving me a stackdump error when I run it? 调试时我的代码运行正常,但是一旦运行它就会崩溃 - My code runs fine when I debug it, but crashes as soon as I run it Qt Creator 在新项目中显示错误,但代码编译正常 - Qt Creator shows errors in fresh project, but code compiles fine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM