在运行时从txt文件更新文本框而不单击“刷新”

时间:2018-05-18 18:28:40

标签: c# wpf xaml

在wpf中,我有一个文本框,其内容来自txt文件。

代码behine中的

看起来像:

pick 2f05aba ... will be preserved
fixup 3371cec ... will be merged with daed25c
fixup daed25c ... will be merged with e2b2a84
pick e2b2a84 .. will include 3371cec and daed25c, but only the commit message of e2b2a84

问题是txt文件内容在运行时期间正在发生变化,是否可以与这些更改同步?我的意思是如果改变txt文件,文本框内容将会改变。如果有可能请提供一些代码示例i c#或XAML。

我现在的解决方案是我制作了一个“刷新”按钮,可以从txt文件中再次读取

4 个答案:

答案 0 :(得分:3)

您可以配置FileSystemWatcher。我添加了FileWatcherConfigure函数作为示例。

每当文件发生变化时,都会发布FileChanged个事件。您应该使用该事件触发RefreshTextbox方法。

FileSystemWatcher fileWatcher = new FileSystemWatcher();
string filePath = @"./Log.txt";
public MainWindow()
{
    InitializeComponent();

    ReadFromTxt();
    FileWatherConfigure();
}

public void FileWatherConfigure()
{
    fileWatcher.Path = System.IO.Path.GetDirectoryName(filePath);
    fileWatcher.Filter = System.IO.Path.GetFileName(filePath);
    fileWatcher.Changed += FileWatcher_Changed;
    fileWatcher.EnableRaisingEvents = true;
}

private void FileWatcher_Changed(object sender, FileSystemEventArgs e)
{
    Thread.Sleep(TimeSpan.FromSeconds(1));

    ReadFromTxt();
}


void ReadFromTxt()
{
    string[] lines = System.IO.File.ReadAllLines(filePath);
    MyTextBox.Dispatcher.Invoke(() => { this.MyTextBox.Text = string.Join(Environment.NewLine, lines); });
}

答案 1 :(得分:2)

您需要一个FileSytemWatcher,它将触发一个可用于自动刷新文本框内容的事件。

我正在使用以下项目来检测新的和更新的文件。

我注意:

  • LastAccess:在文件更改时获取事件
  • LastWrite:在文件更改时获取事件
  • FileName:在重命名与
  • 匹配的文件时获取事件

请注意,我每次都清除文本框以避免重复的行(您的代码会将整个文件附加到现有内容)

public class MyWindow : Window
{
  public MyWindow()
  {
    InitializeComponent();

    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = @"D:\";
    watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName;
    watcher.Filter = "Log.txt";

    // Add event handlers.
    watcher.Changed += new FileSystemEventHandler(OnFileChanged);
    watcher.Created += new FileSystemEventHandler(OnFileChanged);
    watcher.Renamed += new RenamedEventHandler(OnFileRenamed);

    // Begin watching.
    watcher.EnableRaisingEvents = true;

    ReadFromTxt();
  }

  private void OnFileChanged(object source, FileSystemEventArgs e)
  {
    ReadFromTxt();
  }

  private void OnFileRenamed(object source, RenamedEventArgs e)
  {
    ReadFromTxt();
  }

  private void ReadFromTxt()
  {
    // The easiest way is to just replace the whole text
    MyTextBox.Text = "";
    if (System.IO.File.Exists(@"D:\Log.txt") == false)
    {
      return;
    }
    string[] lines = System.IO.File.ReadAllLines(@"D:\Log.txt");
    foreach (string line in lines)
    {
      MyTextBox.AppendText(line + Environment.NewLine);
    }
  }
}

答案 2 :(得分:0)

为了澄清其他答案,FileSystemWatcher可以是一个选项,但您应该明确地将轮询视为替代方案。为了可读性(基于意见)和可靠性(见thread)。请注意,还有许多其他帖子强调了与使用FileSystemWatcher

相关的问题

通过民意调查,我的意思是寻找特定周期时期的变化。一个选项是使用Timer并查找更改并采取相应措施。

答案 3 :(得分:-1)

查看:

<TextBox Text="{Binding myText}"/>

型号:

private string _myText;
public string MyText{
    get { return _myText; }
    set { _myText = value;
          NotifyPropertyChanged();
}

在您的窗口中实施INotifyPropertyChanged 并更改属性MyText而不是MyTextBox

相关问题