How To Get and Set File Info with FileSystem Attributes VB2005

This article and source code demonstrate how to use the .NET 2.0 FileInfo class’ Attributes property and Visual Basic 2005 to get and set file info.

 

The Attributes property of the System.IO.FileInfo class is used to get or set file attributes. For example, you can determine if a file is read-only, make a file read-only, or make a file not read-only.

 

Some commonly used file attributes are Archive, Compressed, Directory, Encrypted, Hidden, Normal, ReadOnly, System, and Temporary. Use the FileInfoAttributes link at the end of this article to see more attributes.

 

Bitwise arithmetic with the And and Or keywords is used to examine or modify a file’s attributes. Because a file can have any combination of attributes, the Attributes property accepts a combination of enumerated values. To learn more about Bitwise arithmetic visit: Logical and Bitwise Operators in Visual Basic (look for the Bitwise Operations section)

 

When first called, FileInfo calls Refresh and returns the cached information on APIs to get attributes and so on. On subsequent calls, you must call Refresh to get the latest copy of the information. Depending on whether the file system has caught up with changes you make with FileInfo, even after a refresh you may get cached results.

The value of this property is a combination of the archive, compressed, directory, hidden, offline, read-only, system, and temporary file attribute flags.

 

Source Code FileInfo Form

 

 

Code Excerpt From Source Code

 

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

 

This example has provided some code patterns that show how to use a FileInfo object and bitwise arithmetic to GET or SET file attributes. Visit the pages listed below to learn more.


Advertisements

~ bởi duriangroup on Tháng Sáu 1, 2007.

 
%d bloggers like this: