0

I have a text file which I would like to include in several solutions. Following the DRY (Don’t repeat yourself) principle, I would like to have only one copy of it. Naturally, I thought of adding it to all projects using add-as-link.

However, that raises a problem of how to reference it in the code. If it were a code file - it would be accessed by using its namespace. However as a text file - though I can find the project directory easily enough - I'd also need to find its path inside the project. Hard coding that would be error prone (-what if I move it later - there's no intellisense for that).

Adding it under Properties.Resources would mean having multiple copies of it - which I'm trying to avoid.

So - how is this done?

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • 1
    If it were a codefile, you'd also have it duplicated in each of the projects (albeit compiled as code), so where's the difference? Are those solutions all shared as part of one application? I'm having trouble understanding your actual problem :) – Luaan Feb 20 '14 at 14:39
  • @Luaan If it were a codefile, recompiling a project would use the latest version of it. – ispiro Feb 20 '14 at 14:40
  • make a PostBuild Event which XCopy s the file from the original location (solution folder) to a relative path under each project. – cleftheris Feb 20 '14 at 14:44
  • 1
    Yes, but the same thing happens when you have build action Content (or Embedded Resource), rather than Compile. – Luaan Feb 20 '14 at 14:48
  • @Luaan I was just explaining why this type of duplication is OK. You are correct that that would be the same for a text file. The problem is, as mentioned in the question, how to reference it in code. – ispiro Feb 20 '14 at 14:50
  • Is managing this through your source-control an option for you? e.g. use `properties.resource` and control the multiple copies through e.g. [TFS](http://blogs.msdn.com/b/jmeier/archive/2007/03/17/code-sharing-in-team-foundation-server.aspx). – richaux Feb 20 '14 at 14:55
  • @cleftheris That would suffer from the same problem as hard-coding the relative path (mentioned in the question). – ispiro Feb 20 '14 at 14:58
  • It's hard to give any advice when we have no idea about your constraints, or what you're actually trying to achieve. Do you want the file to be on the disk, or in the DLL? Can you have a shared library that would contain the file and / or access to its data? Also, you can easily read resources of an assembly using `Assembly.GetManifestResourceStream`. – Luaan Feb 20 '14 at 15:00
  • Imagine I have two projects which are then deployed to two customers' computers. They both have to show a text file's contents (currently: "Don't Panic.") to the user. Then I change my mind and change the file to have "OK. Panic.". I recompile, deploy to users, and they both should see "OK. Panic.". – ispiro Feb 20 '14 at 15:07

1 Answers1

0

Mark it as an embedded resource, then get it from the ResourceStream.

See this question's answers for how to do that.

Community
  • 1
  • 1
ispiro
  • 26,556
  • 38
  • 136
  • 291