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

Mark Sanchez 61 Reputation points
2021-10-07T18:20:05.997+00:00

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.

Thanks

Developer technologies | .NET | .NET Runtime
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2021-10-07T19:30:13.717+00:00

    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,666 Reputation points
    2021-10-08T06:06:40.317+00:00

    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.

    https://khalidabuhakmeh.com/verify-user-file-uploads-with-dotnet

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

    https://www.mikesdotnetting.com/article/277/reading-excel-files-without-saving-to-disk-in-asp-net

    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.

    https://learn.microsoft.com/en-us/dotnet/standard/io/memory-mapped-files

    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.