0

尝试关闭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();
}
4

5 回答 5

6

您需要StreamReadertry.

话虽如此,在这种情况下,我建议使用该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# 方式进行。 StreamReaderIDisposable.Dispose实现将关闭流

于 2013-07-31T17:14:17.767 回答
5

在尝试之前声明你的变量:

StreamReader myReader = null;

等然后将它们设置在 try 块中。

于 2013-07-31T17:16:03.063 回答
2

您需要在 // 块之外声明您的StreamReader实例。trycatchfinally

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();
}
于 2013-07-31T17:13:44.703 回答
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();


    }
}
于 2013-07-31T17:17:10.520 回答
1

只需myReader在 try 块之外定义,并在 finally 块中在调用 close 之前检查 null

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();
}
于 2013-07-31T17:15:11.597 回答