本文介绍了C#中发生异常后恢复操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要解压缩的文件数量为n,但是1或2个文件可能是错误的文件.假设我有三个文件要解压缩,如果第一个文件是一个好文件,我的代码将其解压缩.开始解压缩错误文件的那一刻,代码进入catch块,退出并没有 解压第三个文件,这实际上是一个很好的文件.无论如何,在执行catch语句后,c#中是否可以继续解压缩文件.

I have n number of files to unzip but 1 or 2 files can be a bad file. suppose i have three files to unzip, if the first file is a good file, my code unzips it. the moment it starts unzipping bad file, code goes to catch block and it exits and it doesn't unzip the third file which is actually a good file. Is there anyway in c# to continue unzipping files after catch statement is executed.

谢谢

赛义德

   public void Main()


{
            string directoryPath = Dts.Variables["User::Var_File"].Value.ToString();



                DirectoryInfo directorySelected = new DirectoryInfo(directoryPath);

                try
                {


                    foreach (FileInfo fileToDecompress in directorySelected.GetFiles("*.gz"))
                    {

                        Decompress(fileToDecompress);
                    }
                }

                catch (Exception ex)
                {
                    return;

                }
                string[] fileNames = System.IO.Directory.GetFiles(directoryPath, "*.gz");

                foreach (string f in fileNames)
                {
                    File.Delete(f);
                }



        }

 public static void Decompress(FileInfo fileToDecompress)
        {
            using (FileStream originalFileStream = fileToDecompress.OpenRead())
            {
                string currentFileName = fileToDecompress.FullName;
                string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);

                using (FileStream decompressedFileStream = File.Create(newFileName))
                {
                    using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
                    {
                        decompressionStream.CopyTo(decompressedFileStream);
                        Console.WriteLine("Decompressed: {0}", fileToDecompress.Name);
                    }
                }
            }
}

        }


推荐答案

上述内容应该做什么?返回什么?

What is the above supposed to be doing? Return to what?

您应该寻找特定的异常,不对其进行任何处理,将其吞下并继续执行程序.

You should be looking for the specific exception and doing nothing with it, swallowing it and make program execution continue.

catch ( Exception ex(捕获特定异常 )
  
{
       //不采取任何措施就可以吞下它.

  
}

catch (Exception ex(catch the specific exception)
  
{
        //swallow it by doing nothing.

  
}


这篇关于C#中发生异常后恢复操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 22:31