Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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.
Posted
Comments
OriginalGriff 12-Oct-13 7:40am    
Answer updated

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:
C#
/// <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]
 
Share this answer
 
v2
Comments
Draco2013 6-Apr-16 3:53am    
in vb.net isignorable does not work is there an equivalent to this?
Hi,you need the whole path - something like "C:\inetpub\wwwroot\Testsite\images".

Use
C#
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[^]
 
Share this answer
 
v2
Comments
OriginalGriff 12-Oct-13 7:40am    
Reason for my vote of one: Please read the question. This has nothing to do with Server.MapPath, it is concerned with system folders and the problems you get is reading all files on a disk.

Posting anything just to put an answer in is not a good idea - it starts to look like "points whoring" and abuse. Please do not do it again...
tanweer 12-Oct-13 7:49am    
thanks OriginalGriff, as i tested a sample code on my own by passing path like "D:\test\images" then it gives me same error of access is denied, and when i change the path to Server.MapPath then it works fine for me :) as for my answer is concern i think its good idea to give suggessions to anyone, no matter your all answers are accepted.
OriginalGriff 12-Oct-13 8:03am    
But if you look at the question, it specifically refers to D:\ - the root folder - and that is were the problem lies. The root folder of (almost) every drive contains two system folders, one describing the volume itself, and the recycle bin. You can't access either of these without Admin privileges, and GetFiles always fails when it tries.

Because you use MapPath, you don't see it, because you are working from a logical root, rather than the actual root directory of a physical disk (or a logical partition mounted as a drive).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900