0
这些代码似乎在以前工作,但我没有备份,现在它带有这个问题,我真的不知道为什么。WPF,C#,每次“Textrange.save”方法只能保存一个4096字节的“.text文件”
目的:我想从COM端口接收到的所有串口内容,使用典型的TextRange.save(filestream,DataFormat.Text ) 方法。
这里是一边串行代码,我只是将序列日期的副本放入一个函数中,将内容保存到文件中。
private void Recieve(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
// Collecting the characters received to our 'buffer' (string).
try
{
data_serial_recieved = serial.ReadExisting();
}
catch
{
//MessageBox.Show("Exception Serial Port : The specified port is not open.");
}
Dispatcher.Invoke(DispatcherPriority.Normal, new Delegate_UpdateUiText(WriteData), data_serial_recieved);
/* log received serial data into file */
Tools.log_serial(data_serial_recieved);
}
这是我使用的功能log_serial(串)的唯一地方。
这里来,我救串入文件的代码:
public static void log_serial(string input_text)
{
Paragraph parag = new Paragraph();
FlowDocument FlowDoc = new FlowDocument();
string text = input_text;
string filepath = Globals.savePath
+ "\\" + Globals.FileName_Main
+ ".text";
parag.Inlines.Add(text);
FlowDoc.Blocks.Add(parag);
try
{
using (FileStream fs = new FileStream(@filepath, FileMode.OpenOrCreate, FileAccess.Write))
{
TextRange textRange = new TextRange(FlowDoc.ContentStart, FlowDoc.ContentEnd);
textRange.Save(fs, DataFormats.Text);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
我已经试过了,有这部分也不例外。
问题:每次运行代码时,我在最后得到的文件总是有4096字节的大小。真的不知道是什么导致这个错误,任何人有一个想法,请?
看来它可能是一个特权问题,但是,我第一次使用这些代码时,我确实记得我将所有内容输出到一个.text文件中。这对我来说真的很奇怪。任何帮助?
非常感谢!对于WPF我真的很陌生,我只是写了一些练习(以防我需要那一天)。顺便说一下,可以使用TextRange来追加流文档吗?我无法找到一种方法来做到这一点。 – cmy
最后,为了让我的代码有效,我使用了** FileStream.exist **来检查文件是否存在,并使用** File.CreateText(filepath)**,** File.AppendText(filepath)**和** StreamWriter.Write(文本)**来完成剩下的工作。 – cmy