Skip to main content
 首页 » 编程设计

c#之流媒体编程

2024年02月23日14jyk

我需要学习更多关于流技术的知识。 我正在使用 biztalk 并想开发一些自定义管道组件。 对于性能因素,一切都必须采用基于流的方式。 我收到一条流式消息,但我想在文本中做一些替换, 我现在做的是:

string msg = ""; 
using(StreamReader r = new StreamReader(stream)){ 
     msg = r.readToEnd(); 
} 
 
//do replacements 
 
//send stream away 
StreamWriter... 

如您所见,我在执行 r.readToEnd() 时中断了流。 如何编辑流中的消息?

谢谢

请您参考如下方法:

你不能。您可以从流中读取部分消息,在每个部分中替换您想要的内容,最后将处理过的部分写入另一个流。

使用 ReadToEnd 与流概念相反。我可以建议你应该使用:

using (StreamReader r = new StreamReader(stream)) 
using (StreamWriter w = new StreamWriter(someOutputStream)) 
{ 
    string line = null; 
    while ((line = r.ReadLine()) != null) 
    { 
       line = DoReplacements(line); 
       w.WriteLine(line); 
    } 
}