可能是重复问题:
如何使用C#从回收站还原文件?
在Windows上恢复已删除的文件
我正在开发一个应用程序,旨在从系统中恢复已删除的文件(包括回收站中的文件和已经清空回收站但仍可理解的文件)以及格式化的驱动器。我选择了C#作为编程语言,但我找不到处理这个问题的类。有人知道任何查找已删除文件、检索它们或有关此事的教程或帮助的类/方法吗?我对这个主题的经验很少,所以任何帮助都将受到高度赞赏。
可能是重复问题:
如何使用C#从回收站还原文件?
在Windows上恢复已删除的文件
我正在开发一个应用程序,旨在从系统中恢复已删除的文件(包括回收站中的文件和已经清空回收站但仍可理解的文件)以及格式化的驱动器。我选择了C#作为编程语言,但我找不到处理这个问题的类。有人知道任何查找已删除文件、检索它们或有关此事的教程或帮助的类/方法吗?我对这个主题的经验很少,所以任何帮助都将受到高度赞赏。
没有内置的类可以完成您所要求的操作。
实际上,恢复已删除的文件是一个很难的过程,需要对您的文件系统有非常低级别的了解。因此,首先要做的是获取包含您想要恢复的文件的驱动器的信息。基本上,您首先需要知道它的文件系统。
您将需要大量使用P/Invoke。首先获取到您要定位的驱动器的句柄:
[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();
// Gets a handle to the physical disk
IntPtr hDisk = CreateFile(string.Format("\\\\.\\PhysicalDrive{0}", diskNumber),
GenericRead,
Read | Write,
0,
OpenExisting,
0,
IntPtr.Zero);
CreateFile
获取的句柄,您现在将通过使用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);