Question

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.

Was it helpful?

Solution

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:

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool CloseHandle(IntPtr handle);

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr CreateFile(
    string lpFileName,
    uint dwDesiredAccess,
    uint dwShareMode,
    IntPtr lpSecurityAttributes,
    uint dwCreationDisposition,
    int dwFlagsAndAttributes,
    IntPtr hTemplateFile);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool GetVolumeInformationByHandleW(
    IntPtr hDisk,
    StringBuilder volumeNameBuffer,
    int volumeNameSize,
    ref uint volumeSerialNumber,
    ref uint maximumComponentLength,
    ref uint fileSystemFlags,
    StringBuilder fileSystemNameBuffer,
    int nFileSystemNameSize);

// Gets a handle to the drive
// Note: use CloseHandle to close the handle you opened once work is done
IntPtr hDrive = NativeMethods.CreateFile(
    string.Format("\\\\.\\{0}:", DriveLetter)
    GenericRead,
    Read | Write,
    IntPtr.Zero,
    OpenExisting,
    0,
    IntPtr.Zero);

// Then gets some information about the drive
// The following function requires Vista+
// Use GetVolumeInformation for older systems
const int VolumeNameSize = 255;
const int FileSystemNameBufferSize = 255;
StringBuilder volumeNameBuffer = new StringBuilder(VolumeNameSize);
uint volumeSerialNumber = 0;
uint maximumComponentLength = 0;
uint fileSystemFeatures;
StringBuilder fileSystemNameBuffer = new StringBuilder(FileSystemNameBufferSize);

GetVolumeInformationByHandleW(
    hDrive,
    volumeNameBuffer,
    VolumeNameSize,
    ref volumeSerialNumber,
    ref maximumComponentLength,
    ref fileSystemFeatures,
    fileSystemNameBuffer,
    FileSystemNameBufferSize);

// Now you know the file system of your drive
// NTFS or FAT16 or UDF for instance
string FileSystemName = fileSystemNameBuffer.ToString();

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:

// Gets a handle to the physical disk
IntPtr hDisk = CreateFile(string.Format("\\\\.\\PhysicalDrive{0}", diskNumber),
    GenericRead,
    Read | Write,
    0,
    OpenExisting,
    0,
    IntPtr.Zero);

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 using ReadFile.

// Used to read in a file
[DllImport("kernel32.dll")]
public static extern bool ReadFile(
    IntPtr hFile,
    byte[] lpBuffer,
    uint nNumberOfBytesToRead,
    ref uint lpNumberOfBytesRead,
    IntPtr lpOverlapped);

// Used to set the offset in file to start reading
[DllImport("kernel32.dll")]
public static extern bool SetFilePointerEx(
    IntPtr hFile,
    long liDistanceToMove,
    ref long lpNewFilePointer,
    uint dwMoveMethod);

// Set offset
int bufferSize = 512;
byte[] buffer = new byte[bufferSize];
SetFilePointerEx(
    hDisk,
    offset,
    ref pt,
    FileBegin);

// Read a whole sector
// Note that you can't read less than a whole sector of your physical disk. Usually it's 512 bytes,
// but you'll have to retrieve this information from the disk geometry. If you're interested, I can provide you
// some code. It requires the use of the IOCTL_DISK_GET_DRIVE_GEOMETRY control code.
uint read = 0;
ReadFile(
    hDisk,
    buffer,
    bufferSize,
    ref read,
    IntPtr.Zero);

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top