Search code examples
c#listfileforeachdicom

How to recognize DICOM files? C#


I have a folder with files, but not all of them are DICOM files and I need to recognize which one is a DICOM.

My first decision was to create foreach loop where I am checking Extension:

foreach (var item in fileList.Where(x => x.Extension == ".dcm"))
        {
        }

But not all of them have ".dcm" and some of them do not, but they are DICOM.

I am using fo-dicom nuget package, but didn't find the answer there.

My question is: How to check all files for DICOM?

I know that Dicom file has 128 bytes free and then the last 4 bytes for "DICM", but how I can check this?


Solution

  • If you are using fo-dicom, then you can use the built in method DicomFile.HasValidHeader(filename), that returns true or false.

    this method checks for those magic bytes but also checks for older version of dicom, where the 128 bytes are not present.