打开文件并读取文件内容

 public partial class MainWindow : Window
    {
        private OpenFileDialog openFileDialog = null;
       
        public MainWindow()
        {
           InitializeComponent();
           openFileDialog = new OpenFileDialog();
           openFileDialog.FileOk += openFileDialogFileOk;


        }


        private void openBtn_Click(object sender, RoutedEventArgs e)
        {
            openFileDialog.ShowDialog();
        }


      


        private void openFileDialogFileOk(object sender, System.ComponentModel.CancelEventArgs e)
        {
            string fullPathName = openFileDialog.FileName;
            FileInfo str = new FileInfo(fullPathName);
            fileNameText.Text = str.Name;
            contentText.Text = "";
            TextReader reader = str.OpenText();
            string line = reader.ReadLine();
            while (line != null)
            {
                line = reader.ReadLine();
                contentText.Text +=line +' ';
               
            }
            reader.Close();




        }
    }
If opportunity doesn’t knock, build a door
原文地址:https://www.cnblogs.com/CandiceW/p/4204565.html