How to write data to an embedded resource

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jani Mantytorma

    How to write data to an embedded resource

    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.Reflecti on.Assembly a = System.Reflecti on.Assembly.Get ExecutingAssemb ly();
    System.IO.Strea m stream = a.GetManifestRe sourceStream(th is.GetType(),"S ettings.xml");
    if(!(null==stre am))
    {
    System.IO.Strea mReader rdr = new StreamReader(st ream);
    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.Reflecti on.Assembly a = System.Reflecti on.Assembly.Get ExecutingAssemb ly();
    Stream stream = a.GetManifestRe sourceStream(th is.GetType(),"S ettings.xml");
    m_doc.Save(stre am);
    stream.Close();

    This raises exception, however: "Stream is not writable." What am I doing wrong?
  • Ed Courtenay

    #2
    Re: How to write data to an embedded resource

    AFAIK, embedded resources are read-only and cannot be written to.

    "Jani Mantytorma" <anonymous@disc ussions.microso ft.com> wrote in message
    news:BC0F31F2-A533-4858-AE12-C731FA707192@mi crosoft.com...[color=blue]
    > I have embedded resource called Settings.xml. I'm able to read the[/color]
    resource but how can I write data to the resource. The code for read
    operation follows:[color=blue]
    >
    > private XmlDocument m_doc = new XmlDocument();
    >
    > System.Reflecti on.Assembly a =[/color]
    System.Reflecti on.Assembly.Get ExecutingAssemb ly();[color=blue]
    > System.IO.Strea m stream =[/color]
    a.GetManifestRe sourceStream(th is.GetType(),"S ettings.xml");[color=blue]
    > if(!(null==stre am))
    > {
    > System.IO.Strea mReader rdr = new StreamReader(st ream);
    > m_doc.Load(rdr) ;
    > stream.Close();
    > rdr.Close();
    > }
    >
    > Now, if I make changes to m_doc and want to save them to embedded[/color]
    resource, I try like this:[color=blue]
    >
    > System.Reflecti on.Assembly a =[/color]
    System.Reflecti on.Assembly.Get ExecutingAssemb ly();[color=blue]
    > Stream stream =[/color]
    a.GetManifestRe sourceStream(th is.GetType(),"S ettings.xml");[color=blue]
    > m_doc.Save(stre am);
    > stream.Close();
    >
    > This raises exception, however: "Stream is not writable." What am I doing[/color]
    wrong?


    Comment

    • jjtorma

      #3
      Re: How to write data to an embedded resource

      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.

      "Ed Courtenay" <my-first-name@edcourtena y.co.uk> wrote in message
      news:%23bHgfx5p DHA.648@TK2MSFT NGP11.phx.gbl.. .[color=blue]
      > AFAIK, embedded resources are read-only and cannot be written to.[/color]


      Comment

      Working...