Clangon
Clangon

Reputation: 1408

Adding a file to a project, that will be added to the exe, and be accessable at runtime

I have a file (an xml), that is accessed in my code, I would like it to be some how added to the executable, so my utility can access it at runtime, but still be all in one file. Is there a way to doing that? (C#) Thanks.

Upvotes: 0

Views: 164

Answers (3)

Francis B.
Francis B.

Reputation: 7208

In the properties windows, set the properties Build Action as Embedded Resource.

After that you can access your file like this:

Assembly assbl = Assembly.GetAssembly(this.GetType());
using(Stream s = assbl.GetManifestResourceStream("projectnamespace.embeddedfilename.xml"))
{
    XmlDocument doc = new XmlDocument();
    using (StreamReader reader = new StreamReader(s))
    {
        doc.LoadXml(reader.ReadToEnd());
        reader.Close();
    }
}

In GetManifestResourceStream, you need to specify the "path" of your file in your project.

Upvotes: 2

Sean
Sean

Reputation: 62542

Add it as an embedded resource (set the build action for the file to be "Embedded Resource") and use Assembly.GetManifestResourceStream to access it.

Be aware that when accessing a resource stream the name is case sensitive.

Upvotes: 2

Martin Harris
Martin Harris

Reputation: 28637

Look at embedded resources (first result from a Google search, but looks good at first glance)


Actually this article has the advantage of actually telling you how to make something an embedded resource. Between the two of them you should be able to sort out your problem.

Upvotes: 2

Related Questions