排除隐藏文件和系统文件被扫描 [英] Exclude hidden files and system files from being scanned

查看:76
本文介绍了排除隐藏文件和系统文件被扫描的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在编写文件扫描代码。我想排除隐藏的文件和系统文件被扫描。代码如下。

  var  selectedFiles =  from  file  in  Directory.GetFiles(  D:\\  *。*,SearchOption.AllDirectories)
let info = new FileInfo(file)
where (((info.Attributes & FileAttributes.Hidden)== 0 )&((info.Attributes& FileAttributes.System)== 0 ))
选择文件;

foreach var f in selectedFiles)
listBox1.Items.Add(f);





引发异常并说,访问路径D:\系统卷信息被拒绝。



请更正我。如果有任何其他解决方法,那么请让我告诉它。

我正在使用.NET framework 3.5。

提前谢谢。

解决方案

你可以不是那样的。

问题是SearchOption.AllDirectories总是返回普通代码无法访问的系统文件 - 例如卷信息和回收站。 (这也适用于DirectoryInfo.GetFiles)。



如果你真的需要从任何驱动器的根开始搜索所有文件,那么首先执行root本身(通过排除AllDirectories)然后依次在根目录中的所有子目录上调用带有AllDirectories的GetFiles。讨厌,但这几乎是我所知道的唯一方法。





用一些旧代码挖出来:

  ///   <  摘要 >  
< span class =code-summarycomment> /// 获取所有文件(忽略系统文件夹)
/// < / summary >
/// < param name =path > < / param >
/// < param name =searchPattern > < / param >
/// < 返回 > < / returns > ;
public static string [] GetAllSafeFiles( string path, string searchPattern = *。*
{
List< string> allFiles = new List< string>();
string [] root = Directory.GetFiles(path,searchPattern);
allFiles.AddRange(root);
string [] folders = Directory.GetDirectories(path);
foreach 字符串文件夹 in 文件夹)
{
尝试
{
if (!IsIgnorable(文件夹))
{
allFiles.AddRange(Directory.GetFiles(folder,searchPattern,SearchOption.AllDirectories));
}
}
catch {} // 不知道问题是什么,不在乎...
}
return allFiles.ToArray();
}
/// < 摘要 >
/// 如果这是一个应该被忽略的已知文件夹,则返回true。
/// < / summary >
/// < param < span class =code-summarycomment> name =dir > < / param >
/// < 返回 > < / returns >
private static bool IsIgnorable( string dir)
{
if (dir.EndsWith( System Volume Information)) 返回 true ;
if (dir.Contains(


RECYCLE.BIN)) return true ;
return false ;
}

这是我用来做这项工作的。

[/ edit]


你需要整个路径 - 类似于C:\inetpub \ www.root \Testsite \ images。



使用

 Server.MapPath(  〜/ YourApplication / folder)+  @  \ folder 

获取文件夹的物理路径。

查看这些链接

http://www.dotnetperls.com/mappath [ ^ ]

http://www.dotnetperls.com/physicalapplicationpath [ ^ ]


Hello,

I am writing a code for file scanning. I want to exclude hidden files and system files from being scanned. Code is given below.

var selectedFiles = from file in Directory.GetFiles("D:\\", "*.*", SearchOption.AllDirectories)
                    let info = new FileInfo(file)
                    where (((info.Attributes & FileAttributes.Hidden) ==0)& ((info.Attributes & FileAttributes.System)==0))
                    select file;

            foreach (var f in selectedFiles)
                listBox1.Items.Add(f);



It raises an exception and says, Access to the path 'D:\System Volume Information' is denied.

Please, correct me.If there is any other method of solution then please, let me inform it.
I am using .NET framework 3.5.
Thanks in advance.

解决方案

You can't - not like that.
The problem is that SearchOption.AllDirectories always returns system "files" which normal code will not have access to - the volume info, and the recycle bin for example. (This applies to DirectoryInfo.GetFiles as well).

If you really need to search all files starting from the root of any drive, then do the root itself first (by excluding AllDirectories) and then call GetFiles with AllDirectories on all of the subdirectories in the root in turn. Nasty, but it's pretty much the only way I know of to do it.

[edit]
Dug this out of some old code:

/// <summary>
/// Get all files (ignoring the system folders)
/// </summary>
/// <param name="path"></param>
/// <param name="searchPattern"></param>
/// <returns></returns>
public static string[] GetAllSafeFiles(string path, string searchPattern = "*.*")
    {
    List<string> allFiles = new List<string>();
    string[] root = Directory.GetFiles(path, searchPattern);
    allFiles.AddRange(root);
    string[] folders = Directory.GetDirectories(path);
    foreach (string folder in folders)
        {
        try
            {
            if (!IsIgnorable(folder))
                {
                allFiles.AddRange(Directory.GetFiles(folder, searchPattern, SearchOption.AllDirectories));
                }
            }
        catch {} // Don't know what the problem is, don't care...
        }
    return allFiles.ToArray();
    }
/// <summary>
/// Returns true if this is a known folder that should be ignored.
/// </summary>
/// <param name="dir"></param>
/// <returns></returns>
private static bool IsIgnorable(string dir)
    {
    if (dir.EndsWith("System Volume Information")) return true;
    if (dir.Contains("


RECYCLE.BIN")) return true; return false; }

It's what I use to do that job.
[/edit]


Hi,you need the whole path - something like "C:\inetpub\wwwroot\Testsite\images".

Use

Server.MapPath("~/YourApplication/folder") + @"\folder" 

to get physical path of the folder.
check these links
http://www.dotnetperls.com/mappath[^]
http://www.dotnetperls.com/physicalapplicationpath[^]


这篇关于排除隐藏文件和系统文件被扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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