Getting Only Non-Hidded and Non-System Directories

N

Nathan Sokalski

I am getting the list of subdirectories from a specified directory using
System.IO.Directory.GetDirectories(), and would like to get only
subdirectories that are not hidden or system directories. I did not notice
any way to do this using System.IO.Directory.GetDirectories(). Is there some
other function that I can use, or is there some way to determine whether a
directory is hidden or system? Thanks.
 
T

Tom Shelton

I am getting the list of subdirectories from a specified directory using
System.IO.Directory.GetDirectories(), and would like to get only
subdirectories that are not hidden or system directories. I did not notice
any way to do this using System.IO.Directory.GetDirectories(). Is there some
other function that I can use, or is there some way to determine whether a
directory is hidden or system? Thanks.

You can use System.IO.DirectoryInfo.GetDirectories to return an array
of DirectoryInfo objects. This object has an Attributes property,
which you can check to see if the system or hidden attributes are
applied.
 
N

Nathan Sokalski

Could you show me an example of how I test for these attributes? It seems
like the Attributes property has an enumeration containing Hidden and
System, but I am having trouble testing whether they are true for the
current DirectoryInfo. Thanks.
 
T

Tom Shelton

Could you show me an example of how I test for these attributes? It seems
like the Attributes property has an enumeration containing Hidden and
System, but I am having trouble testing whether they are true for the
current DirectoryInfo. Thanks.






- Show quoted text -

Nathan,

Those attributes are flag attributes - so a simple bitwise and will
tell you if they have the attribute:

Option Strict On
Option Explicit On

Imports System
Imports System.IO

Module Module1


Sub Main()
Dim rootInfo As DirectoryInfo = New DirectoryInfo("c:\")
For Each d As DirectoryInfo In rootInfo.GetDirectories()

If (d.Attributes And FileAttributes.System) <>
FileAttributes.System Then
Console.WriteLine(d.Name)
End If
Next
End Sub


End Module

HTH,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top