C# - Get a list of files excluding those that are hidden
This should work for you:
DirectoryInfo directory = new DirectoryInfo(@"C:\temp");
FileInfo[] files = directory.GetFiles();
var filtered = files.Where(f => !f.Attributes.HasFlag(FileAttributes.Hidden));
foreach (var f in filtered)
{
Debug.WriteLine(f);
}
Get all files except files in hidden directories?
One way without "manually iterating" would be the following:
var dirInfo = new DirectoryInfo(path);
var hiddenFolders = dirInfo.GetDirectories("*", SearchOption.AllDirectories)
.Where(d => (d.Attributes & FileAttributes.Hidden) != 0)
.Select(d => d.FullName);
var files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories)
.Where(f => (f.Attributes & FileAttributes.Hidden) == 0 &&
!hiddenFolders.Any(d => f.FullName.StartsWith(d)));
BUT this will be iterating the whole directory tree twice and has the .Any
-overhead for every file => use @Catburry's solution as it has a better performance and is easier to maintain IMO...
C# Listing files without hidden files (Plus listing option)
I have found this:
This should work for you:
DirectoryInfo directory = new DirectoryInfo(@"C:\temp");
FileInfo[] files = directory.GetFiles();
var filtered = files.Select(f => f)
.Where(f => (f.Attributes & FileAttributes.Hidden) == 0);
foreach (var f in filtered) {
Debug.WriteLine(f);
}
How to use Directory.EnumerateFiles excluding hidden and system files
.Where(f => (new FileInfo(f).Attributes & FileAttributes.Hidden & FileAttributes.System) == 0)
Since FileAttributes
values are flags, they are disjunctive on the bit level, so you can combine them properly. As such, FileAttributes.Hidden & FileAttributes.System
will always be 0
. So you’re essentially checking for the following:
(new FileInfo(f).Attributes & 0) == 0
And that will always be true since you are removing any value with the & 0
part.
What you want to check is whether the file has neither of those flags, or in other words, if there are no common flags with the combination of both:
.Where(f => (new FileInfo(f).Attributes & (FileAttributes.Hidden | FileAttributes.System)) == 0)
You can also use Enum.HasFlag
to make this a bit better understandable:
.Where(f => !new FileInfo(f).Attributes.HasFlag(FileAttributes.Hidden | FileAttributes.System))
Hiding hidden documents and excluding file extensions from a directory in C#
You have a logic issue with:
string[] filePaths = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.xls")
.Where(name => !name.EndsWith(".xlsx") || !name.Contains(@"\~$")
|| (fileAttribute & FileAttributes.Hidden) == 0).ToArray()
you want ands, not ors..
string[] filePaths = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.xls")
.Where(name => !name.EndsWith(".xlsx") && !name.Contains(@"\~$")
&& (fileAttribute & FileAttributes.Hidden) == 0).ToArray()
However, your fileattribute is checking the directory, not the file.
So you actually want
string[] filePaths = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.xls")
.Where(name => !name.EndsWith(".xlsx") && !name.Contains(@"\~$")
&& (File.GetAttributes(name) & FileAttributes.Hidden) == 0).ToArray()
How to find all files, including hidden and system files
DirectoryInfo.GetFiles()
returns all files (excluding those that you don't have permission to see).
At the very least it definitely does include hidden files, as shown by this person who is asking almost exactly the reverse of this question.
Do you have a specific example of a file that appears elsewhere but not in this list?
Exclude certain file extensions when getting files from a directory
You should filter these files yourself, you can write something like this:
var files = Directory.GetFiles(jobDir).Where(name => !name.EndsWith(".xml"));
Hidden folders C#
You can use DirectoryInfo to check if a folder is hidden:
string[] folders = Directory.GetDirectories(path);
foreach (string subFolder in folders) {
string folder = Path.Combine(path, subFolder);
DirectoryInfo info = new DirectoryInfo(folder);
if ((info.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) {
// do something with your non-hidden folder here
}
}
Another solution would be the following one-liner:
var folders = new DirectoryInfo(path).GetDirectories().Where(x => (x.Attributes & FileAttributes.Hidden) == 0);
In this case folders
is an IEnumberable<DirectoryInfo>
.
If you want files instead of directories, simply replace GetDirectories with GetFiles.
Related Topics
Does .Net Provide an Easy Way Convert Bytes to Kb, Mb, Gb, etc.
Is There a Performance Impact When Calling Tolist()
C# Using Parameters.Addwithvalue in SQLdataadapter
Adding Parameters in SQLite with C#
Where Is the Wpf Timer Control
Redirect from Action Filter Attribute
C# .Equals(), .Referenceequals() and == Operator
How to Quickly Check If Two Data Transfer Objects Have Equal Properties in C#
"Open/Close" SQLconnection or Keep Open
Differencebetween Directory.Enumeratefiles VS Directory.Getfiles
Regex to Strip Line Comments from C#
An Attempt Was Made to Access a Socket in a Way Forbidden by Its Access Permissions. Why
Setting Canvas Properties in an Itemscontrol Datatemplate
Null or Default Comparison of Generic Argument in C#