Solved

How to Avoid reading Hidden Folders and Files using C#

Posted on 2015-02-04
5
156 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Perfect! Thank you
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

14 Experts available now in Live!

Get 1:1 Help Now