Solved

How to Avoid reading Hidden Folders and Files using C#

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

785 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