|
以下代码能够卸除D盘的卷,D盘没有读写操作后先将其锁定,然后再将卷卸下,之后打开我的电脑后不能再访问D盘(但是我的电脑中依然能看见“驱动器D”)。但是我想进一步删除盘符D(即在我的电脑中不能再看见D盘),而且能再将D盘装配上去,且里面的数据不能丢失,如何实现呢?
- #include
- #include
- #include
- void main()
- {
- HANDLE hDevice;//handle to the drive to be examined
- BOOL lockResult;//lock results flag
- BOOL dismountResult;//dismount results flag
- BOOL unlockResult;//unlock results flag
- DWORD cbReturned;//discard results
- DWORD dwError;
-
- cout<<"Dismount drive D, Y or N?";
- char chDismount;
- cin>>chDismount;
- if(chDismount == 'Y' || chDismount == 'y')
- {
- //打开卷
- hDevice = CreateFile("\\.\D:",//drive to open
- GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE,//share mode
- NULL, //default security attributes
- OPEN_EXISTING,//disposition
- 0, //file attributes.
- NULL); // don't copy any file's attributes
- if(hDevice == INVALID_HANDLE_VALUE)//can't open the drive
- {
- dwError = GetLastError();
- cout<<"Fail to create file!"<
- return;
- }
- //当驱动器D无读写时,即可锁定卷
- lockResult = DeviceIoControl(
- hDevice,
- FSCTL_LOCK_VOLUME,
- NULL,
- 0,
- NULL,
- 0,
- &cbReturned,
- NULL);
- if(!lockResult)
- cout<<"Fail to lock volume!"<
- else
- {
- //锁定成功即可将卷卸除
- dismountResult = DeviceIoControl(
- hDevice,
- FSCTL_DISMOUNT_VOLUME,
- NULL,
- 0,
- NULL,
- 0,
- &cbReturned,
- NULL);
- if(!lockResult)
- cout<<"Fail to dismount volume!"<
- else
- cout<<"Dismount successful!"<
- }
- }
- }
复制代码
|
|