title: Get size of the File Allocation Table in an exFAT disk using C#
body: <p>I'm writing a program that copies the FAT to a file (and restores it). I'm using <code>CreateFile</code> to open a drive letter as a file, <code>SetFilePointerEx</code> to seek to 0 position, <code>ReadFile</code> to read the contents of the drive, and <code>WriteFile</code> to write to the drive.</p>
  <p>Using this strategy I can basically copy the entire drive. However, how should I know where to start and when to stop? Basically, what I want to know is the location of the beginning and the end of File Allocation Table in an exFAT disk.</p>
  <p>Here's the code that I use to run the backup for the first 4 GB of data:</p>
  <pre><code>
      private static void RunBackup(string driveLetter)
      {
          IntPtr handle = CreateFile(
              string.Format(&quot;\\\\.\\{0}:&quot;, driveLetter),
              FileAccess.Read,
              FileShare.Read,
              IntPtr.Zero,
              (FileMode)OPEN_EXISTING,
              0,
              IntPtr.Zero);
      
          // Set offset
          uint chunks = 100;
          uint bufferSize = 512 * chunks;
          long pt = 0;
          byte[] buffer = new byte[bufferSize];
          SetFilePointerEx(
              handle,
              0,
              ref pt,
              0);
      
          long oneGB = 1073741824;
          var backupSize = oneGB * 4;
          var loops = backupSize / bufferSize;
      
          Console.WriteLine($&quot;Expecting {loops:N0} loops.&quot;);
      
          uint read = 0;
          using (var writer = new BinaryWriter(File.OpenWrite(@&quot;D:\\fat.backup&quot;)))
          {
              for (int i = 0; i &lt; loops; i++)
              {
                  ReadFile(
                      handle,
                      buffer,
                      bufferSize,
                      ref read,
                      IntPtr.Zero);
      
                  writer.Write(buffer);
                  writer.Flush();
      
                  Console.Write($&quot;\rLoop: {i:N0}&quot;);
              }
      
              writer.Close();
          }
      
          CloseHandle(handle);
      }
  
  </code></pre>