Solved

How to Avoid reading Hidden Folders and Files using C#

Posted on 2015-02-04
5
178 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decadeā€¦

732 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