我在尝试关闭Finally
代码块中的文件时遇到错误:
static void Main(string[] args)
{
try
{
StreamReader myReader = new StreamReader("c:\\j\\MyFile1.txt");
string line = "";
while (line != null)
{
line = myReader.ReadLine();
Console.WriteLine(line);
}
//myReader.Close();
}
catch (FileNotFoundException e)
{
Console.WriteLine("sorry file not found! {0}", e.Message);
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine("You have given the wrong path for the file! {0}", e.Message);
}
catch (Exception e)
{
Console.WriteLine("Sorry you typed in a wrong file name! {0}", e.Message);
}
finally
{
myReader.Close();
}
Console.ReadLine();
}
答案 0 :(得分:5)
您需要在StreamReader
之上声明try
。
话虽这么说,我建议在这种情况下使用using
语句而不是try / finally,因为它专门用于资源清理。
using (StreamReader myReader = new StreamReader("c:\\j\\MyFile1.txt"))
{
try
{
string line = "";
while (line != null)
{
line = myReader.ReadLine();
Console.WriteLine(line);
}
}
catch (FileNotFoundException e)
{
Console.WriteLine("sorry file not found! {0}", e.Message);
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine("You have given the wrong path for the file! {0}", e.Message);
}
catch (Exception e)
{
Console.WriteLine("Sorry you typed in a wrong file name! {0}", e.Message);
}
}
Console.ReadLine();
这将保证StreamReader
已关闭,但是以更加自我的C#方式执行此操作。 StreamReader
IDisposable.Dispose
实施将关闭流
答案 1 :(得分:5)
在try之前声明你的变量:
StreamReader myReader = null;
等。然后在try块中设置它们。
答案 2 :(得分:2)
您需要在StreamReader
/ try
/ catch
块之外声明finally
个实例。
static void Main(string[] args)
{
using (StreamReader myReader = null)
{
try
{
myReader = new StreamReader("c:\\j\\MyFile1.txt");
string line = "";
while (line != null)
{
line = myReader.ReadLine();
Console.WriteLine(line);
}
//myReader.Close();
}
catch (FileNotFoundException e)
{
Console.WriteLine("sorry file not found! {0}", e.Message);
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine("You have given the wrong path for the file! {0}", e.Message);
}
catch (Exception e)
{
Console.WriteLine("Sorry you typed in a wrong file name! {0}", e.Message);
}
finally
{
myReader.Close();
}
}
Console.ReadLine();
}
答案 3 :(得分:1)
只需在try块外定义myReader
,最后在调用close
StreamReader myReader = null;
try
{
myReader = new StreamReader("c:\\j\\MyFile1.txt");
//.....
在最后一块
中finally
{
// Performs the operations that should be accomplished for eg closing the connections, file, database
if(myReader != null)
myReader.Close();
}
答案 4 :(得分:1)
这样做
StreamReader myReader = null;
try
{
myReader = new StreamReader("c:\\j\\MyFile1.txt");
string line = "";
while (line != null)
{
line = myReader.ReadLine();
Console.WriteLine(line);
}
//myReader.Close();
}
catch (FileNotFoundException e)
{
Console.WriteLine("sorry file not found! {0}", e.Message);
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine("You have given the wrong path for the file! {0}", e.Message);
}
catch (Exception e)
{
Console.WriteLine("Sorry you typed in a wrong file name! {0}", e.Message);
}
finally
{
// Performs the operations that should be accomplished for eg closing the connections, file, database
if(myReader !=null)
myReader.Close();
}
Console.ReadLine();
}
}