Manipulate file attributes in VB.NET

This example shows how to Manipulate file attribute in VB.NET. The nice feature is, you can brows to the file and it will print the current properties of file and also you can change those properties.

Create a form like below:



Output:



Complete source code: (Please use checkboxes and buttons name default)
------------------------------

Private
Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Button2.Enabled =

False

CheckBox1.Enabled =

False

CheckBox2.Enabled =

False

CheckBox3.Enabled =

False

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Call resetcbox()

OpenFileDialog1.Title =

"Browse the file"

OpenFileDialog1.Filter =

"All files (*.*)|*.*"

If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

alamat = OpenFileDialog1.FileName

Label2.Text = alamat

'to get information size of a file using fileinfo

Dim ukuran As New FileInfo("" & alamat & "")

Label5.Text = ukuran.Length.ToString +

" bytes"

Dim buat As String

'to get information creation time of a file using getcreationtime

buat = File.GetCreationTime(

"" & alamat & "")

Label7.Text = buat

If (File.GetAttributes("" & alamat & "") And FileAttributes.ReadOnly) = FileAttributes.ReadOnly Then

CheckBox1.Checked =

True

End If

If (File.GetAttributes("" & alamat & "") And FileAttributes.Hidden) = FileAttributes.Hidden Then

CheckBox2.Checked =

True

End If

If (File.GetAttributes("" & alamat & "") And FileAttributes.System) = FileAttributes.System Then

CheckBox3.Checked =

True

End If

CheckBox1.Enabled =

True

CheckBox2.Enabled =

True

CheckBox3.Enabled =

True

End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

If MsgBox("Are you sure want to apply change ?", MsgBoxStyle.OKCancel) = MsgBoxResult.OK Then

If CheckBox1.Checked = True And CheckBox2.Checked = False And CheckBox3.Checked = False Then

'to set file attribute readonly

File.SetAttributes(

"" & alamat & "", FileAttributes.ReadOnly)

ElseIf CheckBox1.Checked = True And CheckBox2.Checked = True And CheckBox3.Checked = False Then

'to set file attribute readonly and hidden

File.SetAttributes(

"" & alamat & "", FileAttributes.ReadOnly + FileAttributes.Hidden)

ElseIf CheckBox1.Checked = True And CheckBox2.Checked = True And CheckBox3.Checked = True Then

'to set file attribute readonly ,hidden and system

File.SetAttributes(

"" & alamat & "", FileAttributes.ReadOnly + FileAttributes.Hidden + FileAttributes.System)

ElseIf CheckBox1.Checked = False And CheckBox2.Checked = True And CheckBox3.Checked = False Then

File.SetAttributes(

"" & alamat & "", FileAttributes.Hidden)

'to set file attribute hidden

ElseIf CheckBox1.Checked = False And CheckBox2.Checked = True And CheckBox3.Checked = True Then

'to set file attribute hidden and system

File.SetAttributes(

"" & alamat & "", FileAttributes.Hidden + FileAttributes.System)

ElseIf CheckBox1.Checked = False And CheckBox2.Checked = False And CheckBox3.Checked = True Then

'to set file attribute system

File.SetAttributes(

"" & alamat & "", FileAttributes.System)

ElseIf CheckBox1.Checked = False And CheckBox2.Checked = False And CheckBox3.Checked = False Then

'to set file attribute normal

File.SetAttributes(

"" & alamat & "", FileAttributes.Normal)

End If

Else

Exit Sub

End If

End Sub

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

Button2.Enabled =

True

End Sub

Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged

Button2.Enabled =

True

End Sub

Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged

Button2.Enabled =

True

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

End

End Sub

End

Class

By Perry    Popularity  (5336 Views)