none
[Exch 2010 SP1] [TA] [C#] How to modify a single line in a stream? RRS feed

  • Question

  • I am hoping someone can give me a little help on this, i have a stream of data that looks like this:

    property1:data1
    property2:data2
    property3:data3
    property4:data4
    property5:data5

    I've found all sorts of information on how to read the stream, on how to write a new stream, and on how to append data to the end. But the things i have found that deal with reading the stream, finding a specific property, modifying the that specific string seem so convulted to me. For my particular code, I am using StreamReader and StreamWriter, so maybe that is my problem? If you were going to do it, how would you take a stream that contains the data above, and modify property3 so that it said "property3:data3000"?

    Any guidance greatly appreciated.

    J.
    If it was helpful, vote for it. If it answered your question, mark it as answered. Small thing to do for free help from a strong community :)
    Tuesday, April 12, 2011 1:45 PM

Answers

All replies

  • Generally the method to use in use the TNEFReader and TNEFWriter to create a new TNEF part based on the contents of the current part eg http://blogs.msdn.com/b/mstehle/archive/2009/01/13/howto-sample-transport-agent-add-headers-categories-mapi-props-even-uses-a-fork.aspx. But basically instead of using write all properties you can parse and write each property until you find the one you want modify this and write it in-line. You could use streamreader and streamwriter but you would have to parse the underlying TNEF yourself and depending on what property you modifying you may need to update the property length in line with the stream format as documented in  http://msdn.microsoft.com/en-us/library/cc425498%28v=EXCHG.80%29.aspx but using the TNEFReaders and Writer is going to be far easier.

    Cheers
    Glen

    Wednesday, April 13, 2011 2:43 AM
  • Thank you for the reply and info Glen.  I took your advice and scrapped using the Streamreader/writer approach, but I can't use TNEFReader/writer because my Transport Agent will reside on an edge server.  There is no TNEF for me to parse there.  But, I am specifically intrested in calendar information, so I took your comments and tried again using the CalendarReader and CalendarWriter from the icalendar class.  I believe I am missing some fundamental step in priming the data, for example:

     

     if (e.MailItem.Message.CalendarPart != null)
            {
              MimePart icalpart = e.MailItem.Message.CalendarPart;
              CalendarReader icalreader = new CalendarReader(icalpart.GetContentReadStream());
              //CalendarWriter icalwriter = new CalendarWriter(icalpart.GetContentWriteStream(icalpart.ContentTransferEncoding));
              string body = icalreader.ReadFirstChildComponent().ToString();
              tw.WriteLine(body);
              //tw.WriteLine(icalreader.ComponentName);          
              
            }

    From the code above, I was just hoping to get the reader to write something, anything at all (in this case, the word "true" since readfirstchildcomponent returns a bool value).  I've tried it many ways, using the PropertyReader for example, but no matter what, I always get the same exception:

    Exception
    This operation is not allowed in current reader state.
    System.Collections.ListDictionaryInternal

    I think I'm getting that error because there is no actual data loaded into the reader yet, but I'm not sure how to laod it.  It's really kind of annoying, if you have any advice on getting that working, it would be greatly appreciated.

    J.

     


    If it was helpful, vote for it. If it answered your question, mark it as answered. Small thing to do for free help from a strong community :)
    Thursday, April 14, 2011 6:42 PM
  • I've had that issue before but can't recall or find the fix for that i would suggest having a look at http://msdn.microsoft.com/en-us/library/bb204066(v=EXCHG.140).aspx which is a good example of modifying a message.  If this still doesn't help post back and I'll see if i can track it down I'm on a agent building project for the next couple of weeks I'm sure it will pop up.

    Cheers
    Glen

    Friday, April 15, 2011 1:06 PM