Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to Set File Attributes Using Visual Basic.Net

Author: DevASP
Download Source Code : 340_Get n Set Attribute File.zip

This article is about how you can get and set the attributes of a file like Read only, Hidden, System.

  • Steps that you will do. 
  • Start visual studio and create a new window application  
  • Set the following properties of the form.

Text = “Get n Set Attribute file”

 

  • Drop three check box control on the form and set their properties as below.

First check box

Name = “cbReadonly”

Text = “Read Only”

 

Second check box

Name = “cbHidden”

Text = “Hidden”

 

Third check box

Name = “cbSystem”

Text = “System”

 

  • Now place two label control and set following properties.

First label

Name = “lbFilePath”

Text = “”

Second label

Text = “Path”

 

Third label

Text  = ””

Name = “lbCreationTime”

 

Fourth label

Name = “lbSize”

Text = “”

 

  • Place three buttons and set their properties as below.

First button

Name = “btApply”

Text = “Apply”

 

Second label

Name = “btExit”

Text = “Exit”

 

Third button

Name = “btBrowse”

Text = “Browse”

 

  • Drop a OpenFileDialog control in your designer to brows for the files.  
  • Open your code window and write the following functions.

Private Sub resetcbox()

   cbReadonly.Checked = False

   cbHidden.Checked = False

   cbSystem.Checked = False

End Sub

 

Private Sub reset()

     Call resetcbox()

End Sub

  

  • Declare a variable as below

Dim alamat As String

 

  • Write following code in the load event of the form.

btApply.Enabled = False

cbReadonly.Enabled = False

cbHidden.Enabled = False

cbSystem.Enabled = False

 

  • Write following code in the click event of the browse button.

Call resetcbox()

OpenFileDialog1.Title = "Browse the file"

OpenFileDialog1.Filter = "All files (*.*)|*.*"

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

alamat = OpenFileDialog1.FileName

lbFilePath.Text = alamat

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

lbCreationTime.Text = ukuran.Length.ToString + " bytes"

Dim buat As String

buat = File.GetCreationTime("" & alamat & "")

lbSize.Text = buat

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

cbReadonly.Checked = True

End If

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

cbHidden.Checked = True

End If

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

cbSystem.Checked = True

End If

cbReadonly.Enabled = True

cbHidden.Enabled = True

cbSystem.Enabled = True

End If

  • Write following code in the click event of apply button.

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

If cbReadonly.Checked = True And cbHidden.Checked = False And cbSystem.Checked = False Then

File.SetAttributes("" & alamat & "", FileAttributes.ReadOnly)

ElseIf cbReadonly.Checked = True And cbHidden.Checked = True And cbSystem.Checked = False Then

File.SetAttributes("" & alamat & "", FileAttributes.ReadOnly + FileAttributes.Hidden)

ElseIf cbReadonly.Checked = True And cbHidden.Checked = True And cbSystem.Checked = True Then

File.SetAttributes("" & alamat & "", FileAttributes.ReadOnly + FileAttributes.Hidden + FileAttributes.System)

ElseIf cbReadonly.Checked = False And cbHidden.Checked = True And cbSystem.Checked = False Then

File.SetAttributes("" & alamat & "", FileAttributes.Hidden)

ElseIf cbReadonly.Checked = False And cbHidden.Checked = True And cbSystem.Checked = True Then

File.SetAttributes("" & alamat & "", FileAttributes.Hidden + FileAttributes.System)

ElseIf cbReadonly.Checked = False And cbHidden.Checked = False And cbSystem.Checked = True Then

File.SetAttributes("" & alamat & "", FileAttributes.System)

ElseIf cbReadonly.Checked = False And cbHidden.Checked = False And cbSystem.Checked = False Then

File.SetAttributes("" & alamat & "", FileAttributes.Normal)

End If

Else

Exit Sub

End If

  • Write following line of code in the CheckChange event of the each check box.

btApply.Enabled = True

  • Write following code in the click event of the exit button to exit from application.

End

 

 

 

 

Article Comments
Very Helpfull code to change attributes of a file. thx

Posted on 12/22/2006 2:23:02 AM by Dhirendra S Chilwal

good code

Posted on 12/22/2006 2:24:05 AM by Jeet

Thank you, this saved me some time.

Posted on 2/11/2008 1:54:58 PM by Chuck Smith

helpful... thnks 4 saving my time

Posted on 6/25/2008 8:50:25 AM by alochan

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Make your image Viewer with PictureBox control and ImageList control in VB.Net

Disclaimer - Privacy
© 2002-2017 DevASP.net