Possible Duplicate:
How do I restore a file from the recycle bin using C#?
Recovering deleted file on windows
I am working on an application that is intended to recover deleted files from the system(both those from recycle Bin and those already emptied from the recycle bin but still understandable) and formatted drives. I decided on c# as language, but I have trouble finding classes that deal with this problem. Does anyone know any classes/methods for finding deleted files, retrieving them or any tutorials or helps on the matter. I have little experience on the subject so any help would be strongly appreciated.
There are no built-in classes to do what you asked.
Actually, undeleting files is a hard process and it requires a very low level knowledge of your file system. So the first thing to do is to get information about the drive that contains the file you want to undelete. Basically you first want to know its file system.
You'll have to use P/Invoke a lot. Firstly get a handle to the drive you target:
Once you have the name of the file system, you'll have to manually read raw data from the drive. What you'll read entirely depends on the file system of the drive. Anyway, you'll have to get a handle to the associated hard disk for that:
Now it's the part you'll have to know a lot about your file system... For NTFS file system, you'll have to understand the concept of Master File Table. Actually, that's pretty hard. For FAT file systems, that's less complicated but still, you'll have to study the FS for a while. Start with wikipedia.
From the handle you got using
CreateFile
, you'll now read (raw access) byte per byte (sector by sector actually) into the disk to get the information you want usingReadFile
.For NTFS, first thing it to get the starting sector of the MFT.... then you'll have to "parse" the MFT and look for deleted files...
I won't explain the whole process here. See this link for an example.
So good luck with that :)
Now you probably might want to use a third party application that already does all this stuff and use it from your own program (command line tool as said in the comments).