logo  VB.Net - File Attributes
This Chapter
File Attributes
Binary File
Column Header
Copy Delete ...
CSV File
File Path
Text File
Chapters
Home Page
Colours, RGB
Computer Specifications
Dates&Times
Disk Drives
Files
Folders
GPS and OS Ref
VB.Net Forms
Image Files
If & Select
List/Array
Mathematics
NuGet
Sound
String Functions
Sun and Moon
User Controls
Validation
DigitalDan Sites
My Other Sites
Contact Site

Note
Some pages
may contain
inaccuracies
Hits=5
File Attributes
The attibutes, (hidden, system, directory etc.,) of a file can be obtained via IO.FileAttributes. This uses as set of binary flags to indicate whether an attribute applies to the file.
 

Private Function IsFile_Archive(filename as string) as Boolean
 Return ((IO.File.GetAttributes(filename) And FileAttribute.Archive) > 0)
End Function
Private Function IsFile_Directory(filename as string) as Boolean
 Return ((IO.File.GetAttributes(filename) And FileAttribute.Directory) > 0)
End Function
'
Private Function IsFile_Hidden(filename as string) as Boolean
 Return ((IO.File.GetAttributes(filename) And FileAttribute.Hidden) > 0)
End Function
'
Private Function IsFile_Normal(filename as string) as Boolean
 Return ((IO.File.GetAttributes(filename) And FileAttribute.Normal) > 0)
End Function
'
Private Function IsFile_ReadOnly(filename as string) as Boolean
 Return ((IO.File.GetAttributes(filename) And FileAttribute.ReadOnly) > 0)
End Function
'
Private Function IsFile_System(filename as string) as Boolean
 Return ((IO.File.GetAttributes(filename) And FileAttribute.System) > 0)
End Function
'
Private Function IsFile_Volume(filename as string) as Boolean
 Return ((IO.File.GetAttributes(filename) And FileAttribute.Volume) > 0)
End Function
  

 
File Dates
It is also possible to acquire key dates associated with a file
 

Private Function GetFileCreationTime(filename As String, Optional UTC As Boolean = False) As Date
 ' UTC - true=date returned is in timezone UTC - false = date returned is local time
 Dim inf = My.Computer.FileSystem.GetFileInfo(filename)
 If UTC Then Return inf.CreationTimeUtc
 Return inf.CreationTime
End Function
'
Private Function GetFileLastAccessTime(filename As String, Optional UTC As Boolean = False) As Date
 ' UTC - true=date returned is in timezone UTC - false = date returned is local time
 Dim inf = My.Computer.FileSystem.GetFileInfo(filename)
 If UTC Then Return inf.LastAccessTimeUtc
 Return inf.LastAccessTime
End Function
'
Private Function GetFileLastWriteTime(filename As String, Optional UTC As Boolean = False) As Date
 ' UTC - true=date returned is in timezone UTC - false = date returned is local time
 Dim inf = My.Computer.FileSystem.GetFileInfo(filename)
 If UTC Then Return inf.LastWriteTimeUtc
 Return inf.LastWriteTime
End Function
  

 
Does File Exist and File Length

Private Function GetFileLength(filename As String) As Long
 Return My.Computer.FileSystem.GetFileInfo(filename).Length
End Function
'
Private Function FileExists(filename) As Boolean
 Return IO.File.Exists(filename)
End Function
  

 

DigitalDan.co.uk