使用try中声明的变量使用catch中的值

时间:2018-06-08 13:54:50

标签: c# try-catch

长版:

我被要求开发一个程序,将程序中的文件写入数据库。此外,该目录中更深层次的文件需要写入数据库。它应该显示两列。一个显示路径,另一个显示文件名+扩展名。 我写了一个程序,在测试环境中运行得很好。但是,在实时环境中,我没有打开我想要的每个目录的所有权限。此外,由于一些迁移,多个目录的路径名太长,无法再打开。所以他们告诉我将这些目录写入数据库,但不是在file_name列中显示文件名和扩展名,而是用“无访问权限”填写那个目录。但是,为了避免尝试打开“坏”目录,我使用了try / catch模块。现在我需要在try中声明的变量并在catch中使用它。无论如何这有可能做到吗?我将发布一部分代码,以便您可以看到我现在拥有的内容。

简短版本:

我想在try模块中声明一个变量,并在catch模块中使用它。有没有办法做到这一点?

    public HashSet<String> getFolders(Path path, HashSet<String> folders)
    {                
    try
        {
            foreach (String folder in Directory.GetDirectories(path.getOrigineleMap()))
            {
                Path p = path;
                p.setOrigineleMap(folder);
                folders.Add(folder);
                getFolders(p,folders);
            }
        }
        catch (System.Exception e)
        {
            Console.WriteLine("No access to a folder");
            writeToDatabase(path, folder + @"\No access");   //path is a variable of the type Path, and the String should be the entire
        }                                                    //path from the directory + \No Access
        return folders;
    }

0 个答案:

没有答案
相关问题