tags:

views:

46

answers:

3

You have a library, and one of the function in the library provides a functionality that, among other things output a file (maybe to different paths in every execution). The content and the name of the file is constant in each execution. What is the best way to carry this out? declare the content of the file as a string and print it each time (probably not a good idea, the file is around 1000 lines long)? or have a file which always exist in the same folder as the library dll? How to make sure that the compiler will always include this file? Or is there any better way, for example embedding the file to the dll somehow?

Note that outputting this file is not the only thing that the library do, it's one of the subtasks that is done by a function in the library

Thanks!

+1  A: 

If the content is going to be the same in every case and it's non-trivial (i.e. you couldn't just write it out in a couple of small lines of code) then embed it as a resource file in the assembly.

Then you can just use Assembly.GetManifestResourceStream to get an input stream, and copy the data out to a file. (If you're using .NET 4.0, you can use the Stream.CopyTo method and you're done.)

Jon Skeet
ah I think this is what I'm looking for. How do I embed a resource file? (using VS2010)
Louis Rhys
@Louis: Add the file to the project, click on Properties and set the build action to "Embedded Resource".
Jon Skeet
A: 

You can create a Resource file in your VS2010 project and have typed access to the file as a static member of an automatically generated class. The resource will then be embedded automatically as well.

Try Add New Item -> Resources File. Once the file is created, drag your file onto the designer surface.

erash
A: 

Alternatively there is the file.copy method http://msdn.microsoft.com/en-us/library/system.io.file.copy(VS.71).aspx You can just include the file with your project (Add existing item) and copy it whenever needed.

MaQleod
For this to work, you need to set "Copy to Output Directory" to something besides "Do not copy". I think you might also want to set "Build Action" to "None".
Joe White