我有一个程序,使用户能够在打开的文件对话框中搜索文本文件。然后,用户可以打开他们选择的现有文本文件并对其进行编辑。但是,我的问题是,当他们打开文件时,它显示为空白。我错过了什么?
private void Open_Click(object sender, RoutedEventArgs e)
{
TextBox openText = new TextBox();
var OpenFile = new Microsoft.Win32.OpenFileDialog();
Nullable<bool> Success = OpenFile.ShowDialog();
OpenFile.DefaultExt = ".txt";
OpenFile.Filter = "Text documents (.txt)|*.txt";
if (Success.HasValue && Success.Value)
{
openText.Text = OpenFile.FileName;
}
else
{
//cannot open file
}
}
最佳答案
替换这个:
openText.Text = OpenFile.FileName;
这样:
openText.Text = System.IO.File.ReadAllText(OpenFile.FileName);
关于c# - 在 WPF 和 C# 中打开现有文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46507075/