Recipe 12.11. Getting and Setting File Attributes


You want to view and modify some of the file-level attributes for a specific file.


Sample code folder: Chapter 12\FileAttributes

Use the Attributes property of a file's System.IO.FileInfo object to interact with the attributes defined for that file. You can get a FileInfo object for a specific file through the My.Computer. FileSystem. GetFileInfo() method.


This recipe's sample code lets you view and update the Read Only and Hidden attributes for any specific file.

Begin a new Windows Forms project, and add a TextBox control named FilePath, two Button controls named ActGet and ActSet, and two CheckBox controls named FileReadOnly and FileHidden to Form1. You can add labels and provide meaningful captions if you wish, as is done in Figure 12-7.

Figure 12-7. Controls for the attribute management sample

Set the Enabled properties of the FileReadOnly, FileHidden, and ActSet controls to False. Now add the following source code to the form's class template:

 Private Sub ActGet_Click(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles ActGet.Click    ' ----- Locate the file and its attributes.    If (My.Computer.  FileSystem.FileExists(FilePath.Text) _          = False) Then       MsgBox("Please supply a valid file.", _          MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, _          "Invalid File")       Exit Sub    End If    ' ----- Get the file's attributes.    Dim fileDetail As IO.FileInfo = _       My.Computer.FileSystem.GetFileInfo(FilePath.Text)    FileReadOnly.Checked = fileDetail.IsReadOnly    FileHidden.Checked = CBool(fileDetail.Attributes _       And IO.FileAttributes.Hidden)    FileReadOnly.Enabled = True    FileHidden.Enabled = True    ActSet.Enabled = True End Sub Private Sub ActSet_Click(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles ActSet.Click    ' ----- Modify the settings of the active file.    Dim fileDetail As IO.FileInfo = _       My.Computer.FileSystem.GetFileInfo(FilePath.Text)    ' ----- Set the read-only flag the easy way.    fileDetail.IsReadOnly = FileReadOnly.Checked    ' ----- Set the hidden flag.    If (FileHidden.Checked = True) Then       fileDetail.Attributes = fileDetail.Attributes _          Or IO.FileAttributes.Hidden    Else       fileDetail.Attributes = fileDetail.Attributes _          And Not IO.FileAttributes.Hidden    End If    ' ----- Finished.    MsgBox("Attributes updated.", MsgBoxStyle.OkOnly _       Or MsgBoxStyle.Information, "Attributes") End Sub Private Sub FilePath_TextChanged( _       ByVal sender As System.Object, _       ByVal e As System.EventArgs) _       Handles FilePath.TextChanged    ' ----- Clear the previous file's attributes.    If (ActSet.Enabled = True) Then       ActSet.Enabled = False       FileReadOnly.Enabled = False       FileHidden.Enabled = False       FileReadOnly.Checked = False       FileHidden.Checked = False    End If End Sub 

To use the program, type a valid directory path into the FilePath field, and click the ActGet button. The FileReadOnly and FileHidden fields will update to show the cur-rent attributes for the specified file. Modify these two fields as needed, and then click the ActSet button to modify the file attributes.

The System.IO.FileInfo object abstracts access to all information about a file. Once you have the path to the file, use the following statement to retrieve the FileInfo object:

 Dim fileDetail As IO.FileInfo = _    My.Computer.FileSystem.GetFileInfo(theFilePath) 

The FileInfo object exposes an Attributes property that acts as a bit field for the System.IO.FileAttributes enumeration. (Bit fields use the bitwise operators, including And, Or, and Not, to store multiple enumeration values in a single integer variable.) The FileAttributes enumeration includes several members, but here are the four most commonly used when working with files and directories:

  • FileAttributes.Archive

  • FileAttributes.Directory

  • FileAttributes.Hidden

  • FileAttributes.ReadOnly

This chapter's sample code examines the bits of the FileInfo.Attributes property to determine whether the file is hidden or not:

 FileHidden.Checked = CBool(fileDetail.Attributes _    And IO.FileAttributes.Hidden) 

Since the FileInfo object also exposes a simple IsReadOnly property, the code uses that to set the Read Only flag, although it could have examined the Attributes property for the FileAttributes.ReadOnly bit instead.

Later, those same IsReadOnly and Attributes properties are set with updated values to modify the attributes assigned to the actual file.

Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: