Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何捕获“FileNotFoundException”异常?_C#_Filenotfoundexception - Fatal编程技术网

C# 如何捕获“FileNotFoundException”异常?

C# 如何捕获“FileNotFoundException”异常?,c#,filenotfoundexception,C#,Filenotfoundexception,我对编程非常陌生,正在尝试找出如何捕获错误FileNotFoundException。假设我的代码从文本框中键入的内容中搜索现有文本文档,并将其加载到我的listbox1中。我解决了这个问题。然而,一个新的问题出现了!如果用户输入了错误的名称/编号,则会导致应用程序崩溃,并出现无法找到文件的错误。有没有办法让程序显示错误消息文件未找到。或者干脆不让整个程序崩溃?提前谢谢 private void btnEnter_Click(object sender, EventArgs e)

我对编程非常陌生,正在尝试找出如何捕获错误FileNotFoundException。假设我的代码从文本框中键入的内容中搜索现有文本文档,并将其加载到我的listbox1中。我解决了这个问题。然而,一个新的问题出现了!如果用户输入了错误的名称/编号,则会导致应用程序崩溃,并出现无法找到文件的错误。有没有办法让程序显示错误消息文件未找到。或者干脆不让整个程序崩溃?提前谢谢

     private void btnEnter_Click(object sender, EventArgs e)
    {   
        FileInfo file = new FileInfo(txtExisting.Text + ".txt");
        StreamReader stRead = file.OpenText();
        while (!stRead.EndOfStream)
        {
            listBox1.Items.Add(stRead.ReadLine()); 
        }  
    }
使用语句:

private void btnEnter_Click(object sender, EventArgs e)
{   
    try
    {
        FileInfo file = new FileInfo(txtExisting.Text + ".txt");
        StreamReader stRead = file.OpenText();
        while (!stRead.EndOfStream)
        {
            listBox1.Items.Add(stRead.ReadLine()); 
        }  
    }
    catch (FileNotFoundException ex)
    {
        // Handle exception
    }
}

您应该使用try-catch语句来处理异常

private void btnEnter_Click(object sender, EventArgs args)
{   
    try
    {
        FileInfo file = new FileInfo(txtExisting.Text + ".txt");
        StreamReader stRead = file.OpenText();
        while (!stRead.EndOfStream)
        {
            listBox1.Items.Add(stRead.ReadLine()); 
        }  
    } 
    catch (FileNotFoundException e)
    {
        // FileNotFoundExceptions are handled here.
    }
}
基本上,try块中的代码将按正常方式执行,但如果出现错误,将执行catch块,特别是:

抛出异常时,公共语言运行库CLR将查找处理该异常的catch语句

这意味着,如果您希望遇到不同类型的异常,那么try-catch语句可以有多个catch块,以便可以相应地处理它们

可以找到更多信息


对于用户体验,最好通过显示一条消息与用户沟通出了问题。

只需在btenter\u Click函数中用代码添加一个try/catch块,如下所示:

try
{
   //your code here
}
catch (FileNotFoundException ex)
{
    MessageBox.Show(ex.Message);//if you want to show the exception message
}
catch (Exception ex1)
{
    /* Exceptions other than the above will be handled in this section,
     this should be used when you are not  aware the type of exception 
     can occur in your code for a safe side*/
}
使用System.IO.File.Existpath\\File.extension

它将返回一个布尔值,对于找到的文件为true,对于找不到的文件为false。 如果不知道可能导致问题的原因,请使用try/catch语句

例如:

private void btnEnter_Click(object sender, EventArgs e)
    {   
        if(!System.IO.File.Exists(txtExisting.Text + ".txt")
        {
              MessageBox.Show("File not found");
              return;
        }
        FileInfo file = new FileInfo(txtExisting.Text + ".txt");
        StreamReader stRead = file.OpenText();
        while (!stRead.EndOfStream)
        {
            listBox1.Items.Add(stRead.ReadLine()); 
        }  
    }

应该可以帮助您。与您的问题无关,您确实需要将stRead放入using块中,以便在读取文件后关闭该文件。@AdamHargrave我很高兴:
        FileInfo file = new FileInfo(txtExisting.Text + ".txt");
        if (!File.Exists(file.FullName))
        {
            Console.WriteLine("File Not Found!");
        }
        else
        {
            StreamReader stRead = file.OpenText();
            while (!stRead.EndOfStream)
            {
                lenter code hereistBox1.Items.Add(stRead.ReadLine());
            }
        }