Reading a file stream from resource file in C#

There may be occassions during the course of developing a software project that you may need to store a text file or any binary type file within the resources of project, and make use of it.

In my example below, developing a generic CSV reader class, which is basically wrapper class around a CSV parser utility, LINQtoCSV

What if we wanted to test a few different CSV files at it of various lengths  and check the out put etc. Maybe we need to create some integration tests Unit Tests to esnure it operated as expected, and cover any edge type cases.

Lets set up a test library with nUnit, rhino and other TDD components. We may want to store the CSV files within the Test library, primarily so other developers could execute the tests without having to copy files around.

It’s a simple enough process of adding files to resources, but how do we get them out again?

The wrong solutions

The following solution on StackOverflow. Seems to indicate you need to create some private method and make use of reflection on the current executing assembly.

The solution works, but taking a step back and thinking about it. There has to be a simpler method.

The easy method

A bit of good old-fashioned RTFM! and this page in particular! Adding and Editing Resources (Visual C#)

All that is needed to get a file in the resources and view the properties window and set the File Type to Binary

It is then a trivial matter of passing the byte array to my class in one line of code!

Where FileName is the name of the file in the Resource.

Simple! Easy!

We can now use this in a simple unit test

I appreciate the fact that many purists will take a look at these unit test, and regard them more as integration tests, in that the test is testing actual outcomes of a unit of work, in that there is still a dependency on LinqToCsv being present and that it does some kind of work. However, in my case I did want the ability to test this code, and ensure that the results it emits are as expected. I also wanted a method that I could quickly and repeatedly run these tests.

I would love to hear your views on this. If you feel that you could provide me with a better solution or even improve my current solution I would certainly love to hear it, feel free to contact me or leave a note in the comment section below.


Gary Woodfine

Freelance Full Stack Developer at
Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines.

A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.

Affiliate Disclaimer

Disclosure: Please note that some of the links included in the blog posts are affiliate links, which means I will earn a commission if you decide to make a purchase.

I only include affiliate links to products, services and companies that I have personal experience and have actually used. I only recommend them because they are helpful and useful, not because of the small commissions I make if you decide to buy something.

Please do not spend any money on these products unless you feel they are relevant, suitable and will help you achieve your goals.

Buying anyone of these products and the commisions earned will not make me an overnite multi millionaire, but they will help to cover the hosting costs and compensate for the countless hours and effort I put in to create meaningful and valuable free content I provide to help others succeed.

You've also undoubtedly noticed that I also use various advertising networks - unless of of course you're using an Ad blocker, this is also an attempt to reduce my monthly deficit I experience in providing free relevant, valuable and on occassion insightful content for the benefit of others.

I only really make some money if people click on the ads, unless of course you consider 1c per 1000 impressions real money!

Bear in mind just how many impressions I need to make before I can cover my £20 hosting costs a month!

If you are using an adblocker and guilt stricken you can of course donate using any of the buttons below, but I won't be holding my breath.

Buy me a coffeeBuy me a coffee