#2. How do I " Set the file to compile as an embedded resource?"
This assumes you're using Visual Studio: click on the file in the
solution explorer, and set the Build Action in the properties.
#3. How do I "Read the contents of the file out of the manifest
resources?"
#4. I now have a pre-build step that says "hostname > MachineName.txt"
but it puts the answer in {project}/bin/Debug directory. Should I
have the pre-build step say "hostname > ..\..\MachineName.txt"? or
some other location. I guess question #4 is related to #3.
Yes, they're related. You probably want the text file in the folder
where the project files (.cs, etc.) are stored rather than the output
directory. The key is to make sure that you're writing the machine
name to the file that you're compiling into your assembly. If you're
using VS, you can easily get a handle to it in the pre-build with
macros.
To load a file embedded in the manifest resources, do something like
this:
new
System.IO.StreamReader(myObject.GetType().Assembly.GetManifestResourceStream("Namespace.FileName.ext")).ReadToEnd();
It's probably useful to have something like Lutz Roeder's Reflector
(
http://www.aisto.com/roeder/dotnet/) if you don't already. It's free,
I'm not affiliated with them, and there are probably other tools that
do the same or more. That will allow you to inspect the assembly
you've created to ensure that it contains the resource as well as to
determine the exact string name. Keep in mind the name must be fully
namespace-qualified. Also, the code I wrote above is an example only
and leaves you to deal with things like disposing of resource handles,
exception handling and so on, where necessary.
If you're not using Visual Studio, Mr. Paladino's reply above will
probably yield better results. I've used NAnt but not MSBUILD, so I
don't have much to offer along those lines. Even if you are using VS,
you may be one of the many people who find something like MSBUILD more
convenient than VS. I'd assume you'd somehow need to tell csc or some
linker that you want your text file or other data compiled and
embedded.
Resource files are another perfectly good way to do this. I prefer
this way most of the time because I find it easier. If you're used to
using resx files and so on already, by all means, do it that way. The
general solution should still stand.
s}