c# 打开文件和读取文件内容

        private void button_Click(object sender, EventArgs e)
        {
                string fileName =string.Empty; //文件名
                //打开文件
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.DefaultExt = "txt";
                dlg.Filter = "Txt Files|*.txt";
                if (dlg.ShowDialog() == DialogResult.OK)
                    fileName = dlg.FileName;
                if (fileName == null)
                    return;
                 //读取文件内容
                 StreamReader sr = new StreamReader(fileName, System.Text.Encoding.Default);
                String ls_input = sr.ReadToEnd().TrimStart();
                if (!string.IsNullOrEmpty(ls_input))
                    isopen = true;
                sr.Close();
        }

 

posted @ 2018-01-11 09:12  伏地魔程序员  阅读(12086)  评论(0编辑  收藏  举报