我有一个程序,使用户能够在打开的文件对话框中搜索文本文件。然后,用户可以打开他们选择的现有文本文件并对其进行编辑。但是,我的问题是,当文件打开时,它显示为空白。我错过了什么?
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
}
}
答案 0 :(得分:2)
替换它:
openText.Text = OpenFile.FileName;
用这个:
openText.Text = System.IO.File.ReadAllText(OpenFile.FileName);
答案 1 :(得分:0)
使用File.ReadAllText()
openText.Text = File.ReadAllText(OpenFile.FileName);