我无法再现您的确切问题,我得到的问题是文本文件中只有新文本出现。这是因为StreamReader是一个成员变量,它是一个仅转发的读取器。您可以通过执行以下操作来解决此问题:
public string Read()
{
using (var stream = new FileStream(@"chat.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
using (var reader = new StreamReader(stream))
{
string tmp = "";
string line;
while ((line = reader.ReadLine()) != null)
{
tmp += line + '\n';
}
return tmp;
}
}
}
编辑:问题进一步澄清后:
XAML:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<DockPanel Grid.Column="0" LastChildFill="True">
<Label DockPanel.Dock="Top">IRC channel simulator</Label>
<Button DockPanel.Dock="Top" Click="Button_Click">Send Chat</Button>
<ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Visible">
<TextBox Background="AliceBlue" AcceptsReturn="True" Margin="0,0,0,0" TextWrapping="Wrap" Name="chatWriter" IsReadOnlyCaretVisible="false" />
</ScrollViewer>
</DockPanel>
<ScrollViewer Grid.Column="1" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Visible">
<TextBox IsReadOnly="true" Margin="0,0,0,0" TextWrapping="Wrap" Name="chat" IsReadOnlyCaretVisible="false"/>
</ScrollViewer>
</Grid>
Xaml背后的代码:
public partial class MainWindow : Window
{
ChatHandler chatHandler;
BackgroundWorker worker;
public MainWindow()
{
InitializeComponent();
chatHandler = new ChatHandler();
worker = new BackgroundWorker();
worker.WorkerSupportsCancellation = true;
worker.DoWork += new DoWorkEventHandler(run);
}
private void write(string text)
{
if (chat.Text != text && text != "")
{
chat.Text = text;
}
}
private void run(object sender, DoWorkEventArgs e)
{
BackgroundWorker bg = sender as BackgroundWorker;
while (!bg.CancellationPending)
{
chat.Dispatcher.BeginInvoke((Action)(() => {
write(chatHandler.Read());
}));
Thread.Sleep(100);
}
}
private void chat_Loaded(object sender, RoutedEventArgs e)
{
if (!worker.CancellationPending)
{
worker.RunWorkerAsync();
}
}
private void chat_Unloaded(object sender, RoutedEventArgs e)
{
worker.CancelAsync();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
chatHandler.Write(chatWriter.Text);
}
}
聊天处理程序
public class ChatHandler
{
public FileStream stream;
//StreamWriter writer;
//StreamReader reader;
public ChatHandler()
{
stream = new FileStream(@"chat.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
//writer = new StreamWriter(stream);
//reader = new StreamReader(stream);
}
public void Write(string line)
{
using (var fileStream = new FileStream(@"chat.txt", FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
{
using (var writer = new StreamWriter(fileStream))
{
writer.WriteLineAsync(line);
writer.Flush();
}
}
}
public string Read()
{
using (var fileStream = new FileStream(@"chat/" + DateTime.Today.ToString("d") + ".txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (var reader = new StreamReader(fileStream))
{
string tmp = "";
string line;
while ((line = reader.ReadLine()) != null)
{
tmp += line + '\n';
}
return tmp;
}
}
}
}