Imports System.IO
Public Class ExampleOneForm
Private Sub DemoOneForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Make this form a MDI child of MainForm.
Me.MdiParent = My.Forms.MainForm
End Sub
Private Sub SelectFileButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectFileButton.Click
Dim theOpenFileDialog As New OpenFileDialog
If theOpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim filePath As String = theOpenFileDialog.FileName
If File.Exists(filePath) Then
Me.PathToFileTextBox.Text = filePath
End If
End If
End Sub
Private Sub GetFileIntoButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetFileIntoButton.Click
If Not String.IsNullOrEmpty(Me.PathToFileTextBox.Text) Then
Me.GetFileInfo(Me.PathToFileTextBox.Text)
Else
MessageBox.Show("Please select a file.")
End If
End Sub
Private Sub GetFileInfo(ByVal filePath As String)
Dim doubleSpace As String = Environment.NewLine & Environment.NewLine
Me.ResultsRichTextBox.Clear()
Me.ResultsRichTextBox.AppendText("File Name: " & Path.GetFileName(filePath) & doubleSpace)
' Declare and instantiate a FileInfo object.
Dim theFileInfo As New System.IO.FileInfo(filePath)
' Use Bitwise arithmetic to check a file's ReadOnly attribute.
Dim isReadOnly As String = ""
If (theFileInfo.Attributes And FileAttributes.ReadOnly) = FileAttributes.ReadOnly Then
isReadOnly = "Yes"
Else
isReadOnly = "No"
End If
Me.ResultsRichTextBox.AppendText("Read Only? ".PadRight(25) & isReadOnly & doubleSpace)
' Use Bitwise arithmetic to check a file's Temporary attribute.
Dim isTemporary As String = ""
If (theFileInfo.Attributes And FileAttributes.Temporary) = FileAttributes.Temporary Then
isTemporary = "Yes"
Else
isTemporary = "No"
End If
Me.ResultsRichTextBox.AppendText("Is Temporary File? ".PadRight(25) & isTemporary & doubleSpace)
' Use Bitwise arithmetic to check a file's Compressed attribute.
Dim isCompressed As String = ""
If (theFileInfo.Attributes And FileAttributes.Compressed) = FileAttributes.Compressed Then
isCompressed = "Yes"
Else
isCompressed = "No"
End If
Me.ResultsRichTextBox.AppendText("Is Compressed File? ".PadRight(25) & isCompressed & doubleSpace)
' Use Bitwise arithmetic to check a file's System attribute.
Dim isSystem As String = ""
If (theFileInfo.Attributes And FileAttributes.System) = FileAttributes.System Then
isSystem = "Yes"
Else
isSystem = "No"
End If
Me.ResultsRichTextBox.AppendText("Is System File? ".PadRight(25) & isSystem & doubleSpace)
End Sub
Private Sub ToggleReadOnlyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToggleReadOnlyButton.Click
Dim filePath As String = Me.PathToFileTextBox.Text
If String.IsNullOrEmpty(filePath) Then
MessageBox.Show("Please select a file.")
End If
Me.ResultsRichTextBox.Clear()
' Declare and instantiate a FileInfo object.
Dim theFileInfo As New System.IO.FileInfo(filePath)
' Use Bitwise arithmetic to check a file's ReadOnly attribute.
Dim isReadOnly As String = ""
If (theFileInfo.Attributes And FileAttributes.ReadOnly) = FileAttributes.ReadOnly Then
' Use Bitwise arithmetic to mark the file as Not ReadOnly.
theFileInfo.Attributes = theFileInfo.Attributes And Not FileAttributes.ReadOnly
MessageBox.Show("File was ReadOnly; changed to NOT ReadOnly. Click the Get FileInfo button to see result.")
Else
' Use Bitwise arithmetic to mark the file as ReadOnly.
theFileInfo.Attributes = theFileInfo.Attributes Or FileAttributes.ReadOnly
MessageBox.Show("File was NOT ReadOnly; changed to ReadOnly. Click the Get FileInfo button to see result.")
End If
End Sub
End Class