请问如何编程实现“安全删除”U盘?(63)

  • 主题发起人 主题发起人 ynduanlian
  • 开始时间 开始时间
Y

ynduanlian

Unregistered / Unconfirmed
GUEST, unregistred user!
请问如何编程实现“安全删除”U盘?
 
http://www.birdol.com/article/613.html这是批处理的删除你可以看一下,在程序中执行批处理就行了。。
 
楼上的朋友,打不开这个网页啊。不仅我这边试了打不开,找别的地方的朋友试了也打不开啊
 
高人帮看看,MSDN上的C代码为什么在我机器上不起作用?代码源自: http://support.microsoft.com/kb/165721/zh-cn我在BCB中建立了一个控制台程序:#include <windows.h> #include <winioctl.h> #include <tchar.h> #include <stdio.h> // Prototypes BOOL EjectVolume(TCHAR cDriveLetter); HANDLE OpenVolume(TCHAR cDriveLetter); BOOL LockVolume(HANDLE hVolume); BOOL DismountVolume(HANDLE hVolume); BOOL PreventRemovalOfVolume(HANDLE hVolume, BOOL fPrevent); BOOL AutoEjectVolume(HANDLE hVolume); BOOL CloseVolume(HANDLE hVolume); LPTSTR szVolumeFormat = TEXT("////.//%c:"); LPTSTR szRootFormat = TEXT("%c://"); LPTSTR szErrorFormat = TEXT("Error %d: %s/n"); void ReportError(LPTSTR szMsg) { _tprintf(szErrorFormat, GetLastError(), szMsg); } HANDLE OpenVolume(TCHAR cDriveLetter) { HANDLE hVolume; UINT uDriveType; TCHAR szVolumeName[8]; TCHAR szRootName[5]; DWORD dwAccessFlags; wsprintf(szRootName, szRootFormat, cDriveLetter); uDriveType = GetDriveType(szRootName); switch(uDriveType) { case DRIVE_REMOVABLE: dwAccessFlags = GENERIC_READ | GENERIC_WRITE; break; case DRIVE_CDROM: dwAccessFlags = GENERIC_READ; break; default: _tprintf(TEXT("Cannot eject. Drive type is incorrect./n")); return INVALID_HANDLE_VALUE; } wsprintf(szVolumeName, szVolumeFormat, cDriveLetter); hVolume = CreateFile( szVolumeName, dwAccessFlags, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL ); if (hVolume == INVALID_HANDLE_VALUE) ReportError(TEXT("CreateFile")); return hVolume; } BOOL CloseVolume(HANDLE hVolume) { return CloseHandle(hVolume); } #define LOCK_TIMEOUT 10000 // 10 Seconds #define LOCK_RETRIES 20 BOOL LockVolume(HANDLE hVolume) { DWORD dwBytesReturned; DWORD dwSleepAmount; int nTryCount; dwSleepAmount = LOCK_TIMEOUT / LOCK_RETRIES; // Do this in a loop until a timeout period has expired for (nTryCount = 0; nTryCount < LOCK_RETRIES; nTryCount++) { if (DeviceIoControl(hVolume, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwBytesReturned, NULL)) return TRUE; Sleep(dwSleepAmount); } return FALSE; } BOOL DismountVolume(HANDLE hVolume) { DWORD dwBytesReturned; return DeviceIoControl( hVolume, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &dwBytesReturned, NULL); } BOOL PreventRemovalOfVolume(HANDLE hVolume, BOOL fPreventRemoval) { DWORD dwBytesReturned; PREVENT_MEDIA_REMOVAL PMRBuffer; PMRBuffer.PreventMediaRemoval = fPreventRemoval; return DeviceIoControl( hVolume, IOCTL_STORAGE_MEDIA_REMOVAL, &PMRBuffer, sizeof(PREVENT_MEDIA_REMOVAL), NULL, 0, &dwBytesReturned, NULL); } AutoEjectVolume(HANDLE hVolume) { DWORD dwBytesReturned; return DeviceIoControl( hVolume, IOCTL_STORAGE_EJECT_MEDIA, NULL, 0, NULL, 0, &dwBytesReturned, NULL); } BOOL EjectVolume(TCHAR cDriveLetter) { HANDLE hVolume; BOOL fRemoveSafely = FALSE; BOOL fAutoEject = FALSE; // Open the volume. hVolume = OpenVolume(cDriveLetter); if (hVolume == INVALID_HANDLE_VALUE) return FALSE; // Lock and dismount the volume. if (LockVolume(hVolume) && DismountVolume(hVolume)) { fRemoveSafely = TRUE; // Set prevent removal to false and eject the volume. if (PreventRemovalOfVolume(hVolume, FALSE) && AutoEjectVolume(hVolume)) fAutoEject = TRUE; } // Close the volume so other processes can use the drive. if (!CloseVolume(hVolume)) return FALSE; if (fAutoEject) printf("Media in Drive %c has been ejected safely./n", cDriveLetter); else { if (fRemoveSafely) printf("Media in Drive %c can be safely removed./n", cDriveLetter); } return TRUE; } void Usage() { printf("Usage: Eject <drive letter>/n/n"); return ; } void main(int argc, char * argv[]) { if (argc != 2) { Usage(); return ; } if (!EjectVolume(argv[1][0])) printf("Failure ejecting drive %c./n", argv[1][0]); return ; }我的USB盘为“G”盘,编译后我执行:Project1.exe G结果显示:Media in Drive G has been ejected safely.但我到我的电脑中看见,这个“G”盘依然存在,还能打开访问里面的内容,为什么会这样?
 
接受答案了.
 
后退
顶部