private void btnSelectFile_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "请选择您要打开的文件....";
ofd.Filter = @"文本文件|*.txt|音频文件|*.wav|所有文件|*.*";
ofd.InitialDirectory = @"C:\Users\Administrator\Desktop";
ofd.Multiselect = true;
ofd.ShowDialog();
string path = ofd.FileName;
if (path == "")
{
return;
}
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
{
byte[] buffer = new byte[1024 * 1024 * 5];
int r = fs.Read(buffer, 0, buffer.Length);
txtShowFile.Text = Encoding.Default.GetString(buffer, 0, r);
}
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "请选择您要打开的文件....";
ofd.Filter = @"文本文件|*.txt|音频文件|*.wav|所有文件|*.*";
ofd.InitialDirectory = @"C:\Users\Administrator\Desktop";
ofd.Multiselect = true;
ofd.ShowDialog();
string path = ofd.FileName;
if (path == "")
{
return;
}
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
{
byte[] buffer = new byte[1024 * 1024 * 5];
int r = fs.Read(buffer, 0, buffer.Length);
txtShowFile.Text = Encoding.Default.GetString(buffer, 0, r);
}
}
需要注意的是,FileName的顺序非常关键。必须是在ShowDialog()调用之后才可以取得FileName。