Solved

How to Avoid reading Hidden Folders and Files using C#

Posted on 2015-02-04
5
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 34

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 34

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 23

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

691 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