我就是按书上说的做的,可是每次一打开某个TXT文件,就显示错误说那个TXT不能被ACCESS因为其他程序正在用?
只有下边的两个METHOD, 问题就在ReadFile上好像,因为下边那个mothod 一调用他就扔EXCEPTION.谁能告诉我为什么?谢谢void ReadFile(string filePath)
{
const int MaxTextSize=65535; FileStream fileStream=new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
StreamReader streamReader=new StreamReader(filePath); StringCollection stringCollection=new StringCollection();
int noBytesRead=0;
string oneLine;
while((oneLine=streamReader.ReadLine())!=null)
{
noBytesRead+=oneLine.Length;
if(noBytesRead>MaxTextSize) break;
stringCollection.Add(oneLine);
}
streamReader.Close(); //copy string collection into an array supported by the textbox
string[] stringArray=new string[stringCollection.Count];
stringCollection.CopyTo(stringArray,0);
textBox1.Lines=stringArray;
} private void menuItem2_Click(object sender, System.EventArgs e)
{
OpenFileDialog ofd=new OpenFileDialog();
ofd.InitialDirectory="c:\\";
ofd.Filter="Text files(*.txt)|*.txt|All files(*.*)|*.*";
ofd.FilterIndex=1;
ofd.RestoreDirectory=true; if(ofd.ShowDialog()==DialogResult.OK)
{
try
{   //label1.Text=ofd.FileName.ToString();
ReadFile(ofd.FileName);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"Error reading file");
}
}
}