private void button1_Click(object sender, System.EventArgs e) { Stream myStream = null; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = "c://" ; openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ; openFileDialog1.FilterIndex = 2 ; openFileDialog1.RestoreDirectory = true ; if(openFileDialog1.ShowDialog() == DialogResult.OK) { try { if ((myStream = openFileDialog1.OpenFile()) != null) { using (myStream) { // Insert code to read the stream here. } } } catch (Exception ex) { MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); } } }
FileDialog简单使用demo
最新推荐文章于 2025-10-26 11:09:19 发布
本文介绍了一个使用C#实现的基本文件读取功能。通过一个按钮点击事件,程序会打开一个文件对话框让用户选择一个文本文件。一旦选择了文件,程序尝试打开并读取该文件的内容。
1711

被折叠的 条评论
为什么被折叠?



