Solved

How to Avoid reading Hidden Folders and Files using C#

Posted on 2015-02-04
5
161 Views
Last Modified: 2015-02-05
Hi Experts.
Please help with the lines of code I should be using.

I have the following code righ now:

 
foreach (DirectoryInfo folInfo in dir.GetDirectories())
{
...
}
 {

Open in new window


Now what should I add there to avoid reading hidden folders?


Also I have the same thing for files:

foreach (FileInfo flInfo in folInfo.GetFiles()) 
{
}

Open in new window


How should I change it to only read not hidden files?

Please help.
0
Comment
Question by:vituxa
  • 2
  • 2
5 Comments
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40589261
foreach (DirectoryInfo folInfo in dir.GetDirectories())
{
    if ((folInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) {
             // do something 
    }
    else
    {
             // do somethingelse
    }
}
//
//
//

foreach (FileInfo flInfo in folInfo.GetFiles()) 
{
    if ((flInfo & FileAttributes.Hidden) > 0)
    {
       // do something for hidden file
    }
    else
    {
             // do somethingelse
    }
}

Open in new window

0
 
LVL 1

Author Comment

by:vituxa
ID: 40589295
can I somehow apply a Where clause to folInfo.GetFiles() and dir.GetDirectories() and specify that I only want non-hidden?
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40589347
DirectoryInfo folInfo = new DirectoryInfo(@"C:\Folderpath");
FileInfo[] AllFiles = folInfo.GetFiles();

var folderHidden = folInfo.Select(folder => folder )
                    .Where(folder  => (folder.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden);

foreach (var folder in folderHidden)
{
    // folder variable gives you hidden folder names
}

var fileHidden = AllFiles.Select(file => file)
                    .Where(file => (file.Attributes & FileAttributes.Hidden) == 0);

foreach (var file in fileHidden)
{
    // file variable gives you hidden file names
}

Open in new window


This post has been revised...
0
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 500 total points
ID: 40589734
You can just use the .HasFlag attribute in a linq clause on both items:

            var dir = new DirectoryInfo("xxx");
            foreach (DirectoryInfo folInfo in dir.GetDirectories().Where(d => !d.Attributes.HasFlag(FileAttributes.Hidden)))
            {
                foreach (FileInfo flInfo in folInfo.GetFiles().Where(f => !f.Attributes.HasFlag(FileAttributes.Hidden)))
                {
                }
            }

Open in new window

0
 
LVL 1

Author Closing Comment

by:vituxa
ID: 40591205
Perfect! Thank you
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem of RegEx to match the first occurence of 10 35
Word Directory is not in the drop down list 4 24
Error in JQuery 5 36
Help with C#, MVC, razor. 6 21
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now