Flylib.com

Books Software

 
 
 

Recipe 12.10. Determining If a File Exists


Recipe 12.10. Determining If a File Exists

Problem

You have a file path supplied by the user , but you need to verify that it is valid before using it.

Solution

Use the My.Computer.FileSystem. FileExists() method to determine whether a path string is a valid file or not:

If (My.Computer.FileSystem.FileExists( _
	      userSuppliedPath) = True) Then
	   MsgBox("Invalid file specified.")
	Else
	   ' ----- Process file here.
	End If

Discussion

If you wish to validate a directory instead of a file, use the equivalent DirectoryExists() method:

If (My.Computer.FileSystem.DirectoryExists( _
	      userSuppliedPath) = True) Then
	   MsgBox("Invalid directory specified.")
	Else
	   ' ----- Process directory here.
	End If

See Also

Several of the recipes in this chapter use FileExists() before attempting access to a user-specified path.

Recipe 12.2 discusses the DirectoryExists() method.



Recipe 12.11. Getting and Setting File Attributes

Problem

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

Solution

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.

Discussion

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.