用break退出方法和跑异常的退出递归

   
   private void addCaseFolder()
        {
            var x = new System.Windows.Forms.FolderBrowserDialog();
            if (x.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    recursionFindAllCaseInFolder(x.SelectedPath);
                }
                catch (Exception e)
                {
                    Utils.Logger.Warn(typeof(TaskDetailVM), "Operation failed, please choose local Folder");
                }
                RaisePropertyChanged(() => Cases);
                saveCases();
            }
        }
        private void recursionFindAllCaseInFolder(String FolderPath)
        {
            foreach (var file in new DirectoryInfo(FolderPath).GetDirectories())
            {
                recursionFindAllCaseInFolder(file.FullName);
            }
            foreach (var file in new DirectoryInfo(FolderPath).GetFiles("*.xml"))
            {
                System.Uri uri = new System.Uri(file.FullName);
                if (uri.Host.Equals(System.String.Empty) == false)
                {
                    System.Windows.Forms.MessageBox.Show("Operation failed, please choose local Folder");
                    throw new Exception();
//break;
这里用break:只会推出这个方法,并不会退出整个递归,还是会不断地弹出提示框。因为一个文件在磁盘外,则所有文件都在磁盘外
这里用throw抛异常的方式退出这个方法,即可退出整个递归,如果一个文件在磁盘外,则需要退出整个递归
                }
                Cases.Add(new CaseDetailVM()
                {
                    Case = new TestCaseModel()
                    {
                        CasePath = file.FullName
                    }
                });
            }
        }

你可能感兴趣的:(面向对象语言java+C#)