日期:2014-05-17 浏览次数:20873 次
class FileReadDemo
{
public static void Main()
{
string path;
Console.WriteLine (
"输入要读取的文件名。指定带路径的完整名称:");
path = Console.ReadLine ();
try
{
if (!File.Exists(path))
{
Console.WriteLine("文件不存在");
}
else
{
// 打开流以进行读取。
//创建一个 byte 数组以读取数据
byte[] arr = new byte[100];
UTF8Encoding data = new UTF8Encoding(true);
//继续读文件直到读取文件中的所有数据
while (fs.Read(arr,0,arr.Length) > 0)
{
Console.WriteLine(data.GetString(arr));
}
}
}
catch(Exception ex)
{
Console.WriteLine(“发生错误:" + ex.Message);
}
}
}
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.
}
}
}
&nb