GetFileAttributesA
VB.Net
Imports System.IO
Module Module1
Sub Main()
Dim fileName As String = "C:\path\to\your\file.txt"
Try
Dim attributes As FileAttributes = GetFileAttributes(fileName)
Console.WriteLine("File attributes: " & attributes.ToString())
Catch ex As Exception
Console.WriteLine("Error: " & ex.Message)
End Try
End Sub
Function GetFileAttributes(fileName As String) As FileAttributes
' Use File.GetAttributes method to get file attributes
Return File.GetAttributes(fileName)
End Function
End Module
API
Module Module1
' Constants for file attributes
Private Const FILE_ATTRIBUTE_ARCHIVE As Integer = &H20
Private Const FILE_ATTRIBUTE_DIRECTORY As Integer = &H10
Private Const FILE_ATTRIBUTE_NORMAL As Integer = &H80
Private Const INVALID_FILE_ATTRIBUTES As Integer = -1
' Declare the GetFileAttributesA function from kernel32.dll
<DllImport("kernel32.dll", CharSet:=CharSet.Ansi, SetLastError:=True)>
Private Function GetFileAttributesA(lpFileName As String) As Integer
End Function
Sub Main()
Dim fileName As String = "C:\path\to\your\file.txt"
Try
Dim attributes As Integer = GetFileAttributes(fileName)
If attributes = INVALID_FILE_ATTRIBUTES Then
' If the return value is INVALID_FILE_ATTRIBUTES, an error occurred
Dim lastError As Integer = Marshal.GetLastWin32Error()
Console.WriteLine("Error retrieving attributes. Error code: " & lastError)
Else
' Successfully retrieved attributes
Console.WriteLine("File attributes: " & attributes.ToString())
' Optionally, check for specific attributes
If (attributes And FILE_ATTRIBUTE_DIRECTORY) <> 0 Then
Console.WriteLine("This is a directory.")
End If
If (attributes And FILE_ATTRIBUTE_ARCHIVE) <> 0 Then
Console.WriteLine("This file is marked for backup.")
End If
If (attributes And FILE_ATTRIBUTE_NORMAL) <> 0 Then
Console.WriteLine("This is a normal file.")
End If
End If
Catch ex As Exception
Console.WriteLine("An exception occurred: " & ex.Message)
End Try
End Sub
End Module