Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# KERNEL32.DLL recoverdeleted files

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
static extern IntPtr FindFirstFile(string lpFileName, out WIN32_FIND_DATA lpFindFileData);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern SafeFindHandle FindFirstFile(string lpFileName, out WIN32_FIND_DATA lpFindFileData);
 
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool FindClose(SafeHandle hFindFile);
 
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool FindNextFile(SafeHandle hFindFile, out WIN32_FIND_DATA lpFindFileData);
 
internal sealed class SafeFindHandle : SafeHandleZeroOrMinusOneIsInvalid
{
// Methods
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
internal SafeFindHandle()
: base(true)
{
}
 
public SafeFindHandle(IntPtr preExistingHandle, bool ownsHandle) : base(ownsHandle)
{
  base.SetHandle(preExistingHandle);
}
 
protected override bool ReleaseHandle()
{
if (!(IsInvalid || IsClosed))
{
  return FindClose(this);
}
  return (IsInvalid || IsClosed);
}
 
protected override void Dispose(bool disposing)
{
if (!(IsInvalid || IsClosed))
{
  FindClose(this);
}
base.Dispose(disposing);
}
}
 
private long RecurseDirectory(string directory, int level, out int files, out int folders) {
IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
long size = 0;
files = 0;
folders = 0;
Kernel32.WIN32_FIND_DATA findData;
 
// please note that the following line won't work if you try this on a network folder, like MachineC$
// simply remove the ? part in this case or use ?UNC prefix
using(SafeFindHandle findHandle = Kernel32.FindFirstFile(@"?" + directory + @"*", out findData))
  {
  if (!findHandle.IsInvalid) {
 
  do {
    if ((findData.dwFileAttributes & FileAttributes.Directory) != 0) {
 
      if (findData.cFileName != "." && findData.cFileName != "..") {
        folders++;
 
        int subfiles, subfolders;
        string subdirectory = directory + (directory.EndsWith(@"") ? "" : @"") +
          findData.cFileName;
        if (level != 0)  // allows -1 to do complete search.
          {
        size += RecurseDirectory(subdirectory, level - 1, out subfiles, out subfolders);
 
        folders += subfolders;
        files += subfiles;
        }
      }
    }
    else {
      // File
      files++;
 
      size += (long)findData.nFileSizeLow + (long)findData.nFileSizeHigh * 4294967296;
    }
  }
  while (Kernel32.FindNextFile(findHandle, out findData));
  }
 
}
 
return size;
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: Retrieving a value in one class that is set in another 
Csharp :: Destroy(GameObject.Find("Turret_Laser_Hit"), 0.2f); 
Csharp :: Difference between PhotonNetwork.Ismasterclient and PhotonView.ismasterclient 
Csharp :: We create a PdfDocument, not a (MigraDoc) Document 
Csharp :: asp.net disabled checkbox style 
Csharp :: download xml file asp.net web api 
Csharp :: remote webdriver dotnet 
Csharp :: read barcode with barcode scanner c# winform serial port number 
Csharp :: OBSERVER 
Csharp :: c# half hour dropdown list 
Csharp :: NetConnectionDispatch 
Csharp :: ExecuteResultAsync ActionContext statuscode 
Csharp :: c# convert linq jValue to int 
Csharp :: telerik mvc grid round sum result 
Csharp :: .net disable show exception 
Csharp :: small index c# 
Csharp :: c# string is all zeros 
Csharp :: string extentions not working 
Csharp :: using mediamanager how to play mp3 files 
Csharp :: c# picturebox zoom 
Csharp :: check if app have administrator rights c# 
Csharp :: flutter failed asertion 
Csharp :: C# console out restore 
Csharp :: c# properties making string required 
Csharp :: how to get user control dropdown value in asp net 
Csharp :: tee into file 
Csharp :: how to get the askii code of a char in c# 
Csharp :: visual studio debug copy byte[] from watch 
Csharp :: leave two decimal in double c# 
Csharp :: Write N lines with M numbers each that describe the layout of the second layer in the way shown above 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =