- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 .NET 开发一些网络程序。我想在我的输出窗口上看到整个文本流作为日志。所以我试图搜索在 StreamWriter 上设置多个源的解决方案。但我找不到。我想要像下面这样的样式而不是扩展方法:
var sw = new StreamWriter(socketStream, traceStream)
或
var sw = new StreamWriter(socketStream);
sw.setSecondStream(traceStream);
最佳答案
制作分流器应该不难。对于没有 Seek 函数的只写流,它非常简单。
构造函数调用看起来像
var sw = new StreamWriter(new WriteStreamSplitter(socketStream, traceStream));
和类(class)(未经测试,只是一个模型)
class WriteStreamSplitter : Stream
{
public WriteStreamSplitter(Stream a, Stream b)
{
_streamA = a;
_streamB = b;
}
public override void Write(byte[] buffer, int offset, int count)
{
_streamA.Write(buffer, offset, count);
_streamB.Write(buffer, offset, count);
}
public override bool CanWrite { get { return true; } }
public override bool CanRead { get { return false; } }
// trivial overloads of all other abstract members,
// they are allowed to throw NotImplemented
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
// Maybe it's best to do nothing here but a StreamWriter
// assumes 'ownership' of the wrapped stream,
// so if we want to continue that pattern:
// the guidelines say these shouldn't throw
_streamA.Dispose();
_streamB.Dispose();
}
}
}
关于c# - 如何在 .NET 中将 StreamWriter 的源设置为多个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33935411/
StreamWriter.Flush() 和 StreamWriter.Close() 在功能上有什么区别? 当我的数据没有正确写入文件时,我将 Flush() 和 Close() 添加到我的代码末尾
我需要尽快写入许多文件,但我没有像我使用的那样使用 Streamwriter 获得我想要的结果。例如,如果我写... $File = "c:\somefile" $Contents = get-con
我正在尝试创建一个程序,将文本文件中的几行从文本框编辑为用户设置值。 至少,在我开始使用这个值设置之前。我什至无法编辑该文件。这段代码有什么问题?我实际上尝试了更多示例,但没有一个起作用。 priva
遇到一个问题,streamwriter 在我正在生成的 csv 中生成错误的字符。字符  只出现在文件的开头: 5,"GEN",555555555,,"Evan","Smith",,,,,
我正在尝试创建一个程序,从文本框中将文本文件中的几行编辑为用户设置的值。 至少,在我懒得开始这个值设置之前。我什至无法编辑文件。这段代码有什么问题?我实际上尝试了更多示例,但没有一个有效。 priva
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
如何写入不同类的文件? public class gen { public static string id; public static string m_graph_file; } s
我正在使用 StreamWriter 类将一些文本写入新创建的文本文件,但输出总是出奇地错误,这是我的代码: StreamWriter sw = new StreamWriter("newtextfi
basicLog 是名称和时间戳的列表。我想将它们写入文件。我得到的两个错误是在 ';'在 StreamWriter 行和"file"上。在第二行。 ';'错误是:可能错误的空语句文件错误是:当前上下
这是我的代码。 : FileStream fileStreamRead = new FileStream(pathAndFileName, FileMode.OpenOrCreate, FileAcc
我已经初始化了 StreamWriter 实例并将其与“MyFile.txt”相关联 System.IO.StreamWriter sw = new StreamWriter("MyFile.txt"
我在字符串变量中有大量内容,我想使用流写入器将该内容写入文本文件。但是流编写器正在截断文本而不是将整个内容写入文件。为什么? using (StreamWriter sw = new StreamWr
我有一种方法可以将文件作为附件发送。我使用 StreamWriter 和 MemoryStream 来创建附件。代码如下: public void ComposeEmail(string from,
我试图每小时创建一个新的日志文件,并在服务器上运行以下代码。当天的第一个日志文件正在创建并写入正常,但当天没有创建更多的日志文件。任何想法可能出了什么问题?也不会抛出异常。 private void
我正在为我的游戏制作一个关卡编辑器,大部分工作正常,除了...当我尝试保存我的文件 (XML) 时,文件没有创建,在输出框中我得到: “System.NullReferenceException”类型
我在 form1 中有一个文本框和一个按钮,当我在文本框中书写时,我可以使用我的按钮将其保存到计算机上的文件中。这是放在我的按钮里面 public void button1_Click(obj
我目前的申请有问题,我开始认为这只是我的逻辑。即使浏览了这些表格和 MSDN,我也无法弄清楚。 我正在尝试使用 StreamWriter 在我的应用程序目录中创建文本文档并创建包含该文档的子文件夹。目
我组织了一个小类来实现一个快速记录器。我用一个在构造函数中实例化的私有(private) System.IO.StreamWriter 组成它。因为我使用的方式阻止我实现 using block ,所
我有一个使用此方法的 C# .NET 3.5 Windows 窗体程序: private void toLog(string sLog) { richTextBox1.
这里真的很迷茫。我正在运行 Windows 7 并使用管理员帐户,但由于某种原因,以下代码正在下降; public static readonly string Report = Path.Combi
我是一名优秀的程序员,十分优秀!