C# FileStream(),构造函数

来源:百度知道 编辑:UC知道 时间:2024/07/04 06:27:11
请问FileStream()使用时,如我创建了一个Form应用程序,实例话FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read); 其中应该怎么做才能读取本程序中的文件
我之前做的是s2 = new FileStream(this.Path, FileMode.Open, FileAccess.Read); this.Path为
this.Path=@"Manage.Dat",但是出错了,为什么?应该怎么该

读取本程序中的文件?我看看再说
程序中的文件?要是那样就直接一个txt文件,那就直接可以从资源那提取,而且直接是string类型

private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK) //选择路径
{
using (FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read))
{
//to do something
using (StreamReader sr = new StreamReader(fs, Encoding.Default))
{
//to do something
}
}
}
}

我过去回答过一个问题你可以看看
http://zhidao.baidu.com/question/83842883.html

出错原因可能是程序找不到这个文件
改成this.Path=@"\Manage.Dat"并把这个文件放到程序相同的目录中试试
不行就放到任意盘中,路径改成this.Path=@"X:\Manage.Dat"

先检查文件是否存在
if(File.Exists(this.Path))
{
using(FileStream s2 = new FileStream(this.Path, FileMode.Open, FileAccess.Read))
{
s2.Read()
}