在受限访问文件夹中搜索 [英] search in restricted access folders

查看:105
本文介绍了在受限访问文件夹中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想列出程序可以访问的所有文件和文件夹,并将它们写入文本文件.我写了一种方法可以帮助我做到这一点,但是在某些情况下(文件夹被拒绝访问)我的程序停止工作. 我在这里进行了很多搜索,找到了一些说使用try/catch和其他方法的链接,但我还不能解决我的问题.

I want to list all files and folders that my program has access to and write them to a text file. i write a method which help me do this but in some cases (folder is access denied) my program stop working. i search here a lot and find some links that said use try/catch and sth else but i cant fix my problem yet.

string spcdirectorypath = @"C:\Users";
            string spcfiletape = "*.*";
            DirectoryInfo d = new DirectoryInfo(spcdirectorypath);//Assuming Test is your Folder
            FileInfo[] Files = d.GetFiles(spcfiletape, SearchOption.AllDirectories); //Getting Text files
            foreach (FileInfo file in Files)
                {
                    string str = file.FullName + "\n";
                    richTextBox1.AppendText(str);
                }

现在我该如何解决此问题?还是我可以访问该受限文件夹?对此重复的问题感到抱歉,并感谢您提供最佳答案:)?

now how can i fix this problem? or in other hands can i access to that restricted folders? sorry for this repetitive Q and thanks for best answers :)?

推荐答案

如果要避免未经授权的访问异常,则应在尝试列出文件之前检查权限.

You should check th permissions before trying to list the files, if you want to avoid unauthorized access exceptions.

在这里,我适合您的情况关于行走的MSDN示例目录树:

Here I adapt for your case the example of MSDN about walking a directory tree :

void YourMethod()
{
   string spcdirectorypath = @"C:\Users";
   DirectoryInfo d = new DirectoryInfo(spcdirectorypath);

   WalkDirectoryTree(d)
}

static void WalkDirectoryTree(System.IO.DirectoryInfo root)
{
    System.IO.FileInfo[] files = null;
    System.IO.DirectoryInfo[] subDirs = null;

    // First, process all the files directly under this folder 
    try
    {
        files = root.GetFiles("*.*");
    }
    // This is thrown if even one of the files requires permissions greater 
    // than the application provides. 
    catch (UnauthorizedAccessException e)
    {
        // You may decide to do something different here. For example, you 
        // can try to elevate your privileges and access the file again.
    }

    catch (System.IO.DirectoryNotFoundException e)
    {
        // You may decide to do something different here. For example, you 
        // can log soething.
    }

    if (files != null)
    {
        foreach (System.IO.FileInfo fi in files)
        {
            // In this example, we only access the existing FileInfo object. If we 
            // want to open, delete or modify the file, then 
            // a try-catch block is required here to handle the case 
            // where the file has been deleted since the call to TraverseTree().
            string str = fi.FullName + "\n";
            richTextBox1.AppendText(str);
        }

        // Now find all the subdirectories under this directory.
        subDirs = root.GetDirectories();

        foreach (System.IO.DirectoryInfo dirInfo in subDirs)
        {
            // Resursive call for each subdirectory.
            WalkDirectoryTree(dirInfo);
        }
    }            
}

这篇关于在受限访问文件夹中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆