.NET way to verify if a file can be read?

Mark Sanchez 61 Reputation points

Is there a programmatic way to verify if a file can be read with .NET?

Our use case is we have a set of random files we stream out from restored SQL databases, that are contained inside BLOB fields (image data type). Once the files reside on disk, we want to know if the files are valid without having to open each file manually using its associated application. The file types we are randomly selecting are of the following types: doc/docx, xls,xlsx, pdf, jpg, tif, zip, and dwg.


.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,137 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 60,866 Reputation points

    depends on what you mean by verify. you can use directory functions to check ownership, access and file size. To validate the contents you need to actually read them.

  2. Sreeju Nair 12,181 Reputation points

    You can verify the file contents using the code and ensure the contents before you save it to disk. For e.g. you may verify the file signatures by looking into the signature bytes.

    Refer the following.


    There are many samples available out there that read the data from uploaded file without saving it into disk. Refer an example below.


    Now you may write your applicaiton to read the types of files you want and perform the verification. One helpful class is MemoryMappedFile that will help you to load file contents to memory and work on it.


    For each file type, you many need to find the corresponding libraries that helps you to get the file contents. There are many available, some are commercial. The following are some recommendations based on my past experience.

    Word - https://help.syncfusion.com/file-formats/docio/overview
    Excel - https://help.syncfusion.com/file-formats/xlsio/overview
    Pdf - https://itextpdf.com/en/products/itext-7/itext-7-core
    Images - https://devblogs.microsoft.com/dotnet/net-core-image-processing/

    Hope this helps.

    0 comments No comments