How to write data to an embedded resource

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have embedded resource called Settings.xml. I'm able to read the resource but how can I write data to the resource. The code for read operation follows:

private XmlDocument m_doc = new XmlDocument();

System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream stream = a.GetManifestResourceStream(this.GetType(),"Settings.xml");
if(!(null==stream))
{
System.IO.StreamReader rdr = new StreamReader(stream);
m_doc.Load(rdr);
stream.Close();
rdr.Close();
}

Now, if I make changes to m_doc and want to save them to embedded resource, I try like this:

System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
Stream stream = a.GetManifestResourceStream(this.GetType(),"Settings.xml");
m_doc.Save(stream);
stream.Close();

This raises exception, however: "Stream is not writable." What am I doing wrong?
 
AFAIK, embedded resources are read-only and cannot be written to.

Jani Mantytorma said:
I have embedded resource called Settings.xml. I'm able to read the
resource but how can I write data to the resource. The code for read
operation follows:
private XmlDocument m_doc = new XmlDocument();

System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream stream = a.GetManifestResourceStream(this.GetType(),"Settings.xml");
if(!(null==stream))
{
System.IO.StreamReader rdr = new StreamReader(stream);
m_doc.Load(rdr);
stream.Close();
rdr.Close();
}

Now, if I make changes to m_doc and want to save them to embedded resource, I try like this:

System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
Stream stream = a.GetManifestResourceStream(this.GetType(),"Settings.xml");
m_doc.Save(stream);
stream.Close();

This raises exception, however: "Stream is not writable." What am I doing
wrong?
 
Thanks for your reply. I slept over night and thought that this could be a
reason. And now you confirmed that.

Now I'm wondering why this (read-only) is not emphasized in the MSDN and in
tutorials. But, indirectly they say it: there are no examples of writing to
an embedded resource.

Embedding a resource file would have been very handy if writing had been
allowed.
 
Back
Top