打开文件并读取文件内容

 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 +'\n';
               
            }
            reader.Close();




        }
    }

转载于:https://www.cnblogs.com/CandiceW/p/4204565.html

你可能感兴趣的:(打开文件并读取文件内容)