Reading a file stream from resource file in C#

I have been writing a generic CSV reader class, basically wrapper class for  my favourite CSV parser utility, LINQtoCSV,  into a generic library so I could reuse it easily in my current project. I managed to bang the class out fairly quickly.

Basically the class was ready and I needed to write a few tests just to check it.  I wanted to be able to throw a few different CSV files at it of various lengths  and check the out put etc.  No problem!  I set up a test library with nUnit, rhino and all my other favourite TDD stuff.   I also wanted to have  the CSV files travel with the Test library,  I thought I’d use the resources file to store them, it seemed like the logical place, and I thought it would be a whizz to get  the files from the resources. I’d be finished this in a couple of minutes later and I could reward myself with a nice cup of coffee, before I start the next challenging part of the project.

Then my problem started! I started tapping away, then realised! I don’t know how to read a file from the resources! I hadn’t done this in a long time, in fact I couldn’t remember when the last time was I had actually done this!  No problem, I’ll wizz over to Google & StackOverflow and I’m sure I’ll solve this problem quick smart.  Well that was wishful thinking!  I found this little code sample and for the life of me couldn’t get it to work.

apparently, you’re just to supposed to pass in a string of what file name is.  Something in the order of “Namespace.Properties.FileName.Extension”, rumoured on the forums to work!  I tried every which way but loose! but nothing I passed in worked!  I started down that road to self-destruction every programmer goes through at least 1 or 2 a day!  My receding hairline start to recede further!  Nothing I tried worked! 10 minutes, turned into 20 then to thirty!  I only wanted to read my CSV file into a byte array so I could pass it to my function, so I could test it! It couldn’t be this hard could it?

As it turns out it wasn’t!  All I needed to do was a bit of good old-fashioned RTFM! and this page in particular! , then the answer came right at me, and boy did I feel like a schmuck of the tenth degree! The answer was evidently frustratingly simple!

All I needed to do was go to my file in the resources and view the properties window and set the File Type to Binary


It was then a trivial matter of passing the byte array to my class in one simple line of code!  So that line could look like this

Where FileName is the name of my file in the Resource. Simple! Easy! any idiot could do it! Well apparently not this idiot, without spending 45 Minutes of his life in a state of mental agony!

So with that little nugget of information, safely stored away in an area where I will be able to access it again, I managed to complete my simple Unit Test Case, and successfully determine that my class actually works!

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.


Posted in C#, Geek, Programming
  • m2o2r2g2

    FYI after you read the manual you quoted ‘ Resources.Properties.FileName’ but then in the unit test you quoted ‘Properties.Resources.test’.

    • Gary Woodfine

      Thanks for the taking the time to comment.

      I thought it would be clear enough, where I took time to add the lines:

      “Where FileName is the name of my file in the Resource. Simple! Easy! any
      idiot could do it! Well apparently not this idiot, without spending 45
      Minutes of his life in a state of mental agony!”

      test is the name of the file that I added to Resources, I assumed this would be apparent, with the screen shot I added of the file in resources?

      I referenced the file correctly in my test , i.e. Properties.Resources.test

      test is the name of the file in resources. Apologies if this was clear enough

Follow me on Twitter

%d bloggers like this: