Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6346859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:05:59+00:00 2026-05-24T21:05:59+00:00

I have some C# code that uses FileStreams to open a PhysicalDrive and take

  • 0

I have some C# code that uses FileStreams to open a PhysicalDrive and take an image of the whole disk, but s consistently throwing an IOException with the message “Data error (cyclic redundancy check).” After copying about 121MB of a 128MB disk.

    using Microsoft.Win32.SafeHandles;
    using System.IO;
    using System;

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

public void MakeImage() 
{

    SafeFileHandle TheDevice = null;
    try
    {
    TheDevice = CreateFile(@"\\.\PHYSICALDRIVE1", (uint)FileAccess.Read, (uint)0, IntPtr.Zero, (uint)FileMode.Open, (uint)FILE_ATTRIBUTE_SYSTEM, IntPtr.Zero);
        if (TheDevice.IsInvalid) { throw new IOException("Unable to access drive. Win32 Error Code " + Marshal.GetLastWin32Error()); }
        using (FileStream Dest = System.IO.File.Open("output.bin", FileMode.Create))
        {
            using (FileStream Src = new FileStream(TheDevice, FileAccess.Read))
            {
                Src.CopyTo(Dest);
                Src.Close();
            }
            Dest.Close();
        }
    }
    catch(Exception Ex)
    {
        //Here is where i am getting the IOException
        //Handle error..
    }
    finally
    {
        if (TheDevice != null)
        {
            if (!TheDevice.IsClosed)
                TheDevice.Close();
            TheDevice.Dispose();
        }
    }
}

I have run Scan Disk on the Drive in question and there doesn’t appear to be anything wrong with it. If I change the first Param of CreateFile to just read a partition (not what I want to do), then the image is created fine.

This is a follow up to Windows C# implementation of linux dd command that I’ve been trying to do.

UPDATE:

Further investigations show that the error has something do with not being able to get or know the Src.Length property. I changed my code to copy byte by byte and keep a count, it errors after 126959616 bytes, which is 1 byte more than the total size of the image produced by dd.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:06:01+00:00Added an answer on May 24, 2026 at 9:06 pm

    The cause of the IOException was reading past the end of the Stream. For some reason when opening a Physical Disk as a FileSteam I am unable to get the Length of the stream or otherwise find out its size. The solution is to use the System.Management namespace objects to find out the length of the Device file in bytes and then use Read to copy the correct amount of data from the Stream.

    I think the reason this would happen after 121MB is due to something along the way not using 1024B to a KB and instead using 1000B to a KB, so that was a bit of a red herring on my part.

    I still don’t understand why it would throw a CRC error and not a “Sector Not Found” or “Read Past the End of the Stream” type message.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that uses Open XML to open up a .docx file,
I have some working code that uses IImgCtx to load images, but I can't
I have some code that uses a form.submit() to open up another window when
I have some code that uses: __sync_bool_compare_and_swap it compiles fine on Linux. But when
I have some code that uses the shared gateway pattern to implement an inversion
I have some code that uses the Oracle function add_months to increment a Date
I have some Java code that uses curly braces in two ways // Curly
I have some old code that uses qsort to sort an MFC CArray of
We have some code kicking around that uses this old internal Sun package for
I have some c(++) code that uses sprintf to convert a uint_64 to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.