[英]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.