gpt4 book ai didi

c++ - 如何跟踪或检测 USB 设备的格式化以及如何使用 C++ 知道它已完成格式化?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:13:05 25 4
gpt4 key购买 nike

我想创建一个用户权限的 EXE,它可以检测 USB 设备的格式化,并通知设备开始格式化,完成后显示格式化完成的消息。

使用以下示例代码创建了一个 C++ 控制台应用程序。但它需要管理员权限。

#include "stdafx.h"
#include <fstream>
#include <windows.h>
#include <iostream>
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
char *Fpath="D:\\$Extend\\$RmMetadata\\$TxfLog\\$TxfLog.blf";
std::ifstream is;
while(true)
{
is.open(Fpath);
if(is.is_open())
{
std::cout<<"Waiting for format\n";
is.close();
}
else
{
std::cout<<"formating device\n";
}
Sleep(1000);
}
getchar();
return 0;
}

最佳答案

I got the answer


#include "stdafx.h"
#include <fstream>
#include <windows.h>
#include <iostream>
#include <stdio.h>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
while(true)
{
WIN32_FIND_DATA data;
HANDLE h = FindFirstFile(L"D:\\*.*",&data); // specify the drive letter

if( h!=INVALID_HANDLE_VALUE )
{
cout << "Waiting\n";
}
else
cout << "Formatting\n";
FindClose(h);
Sleep(1000);
}
getchar();
return 0;
}

关于c++ - 如何跟踪或检测 USB 设备的格式化以及如何使用 C++ 知道它已完成格式化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37717244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com