How to Set Attributes in Visual Basic

Written by kevin walker
  • Share
  • Tweet
  • Share
  • Email

Attributes are used by Windows to keep track of special information about the role played by files. Common attributes include "read only," which specifies that a user cannot modify a file, and "hidden," which prevents file browsers like Explorer from displaying the file within a directory. Normally, these attributes can be set by a user in the file properties of Windows Explorer. But a programmer can modify file attributes within a Visual Basic program using the SetAttr function.

Skill level:
Moderately Easy

Other People Are Reading


  1. 1

    Open a new Visual Basic project by clicking "File" and "New Project." Select "Console Application." This brings up a code editor in which users can enter the code. They can use the same code later, without modification, in graphical user interface programs.

  2. 2

    Change the most common file attributes, using Visual Basic shortcuts, by typing the following code:

    SetAttr("c:\csv.txt", vbReadOnly)

    SetAttr("c:\csv.txt", vbHidden)

    SetAttr("c:\csv.txt", vbSystem)

    SetAttr("c:\csv.txt", vbNormal)

    Each time you set an attribute, it overwrites the previous attribute. The above example sets the file "csv.txt" as read-only, then unsets the read-only flag and sets it as hidden, then makes it a system file, then a normal file.

  3. 3

    Use the following code to combine attributes:

    SetAttr("c:\csv.txt", vbHidden + vbReadOnly + vbSystem)

    This uses the "+" operator to indicate that the "csv.txt" file should be flagged as hidden, read only, and a system file.

  4. 4

    Paste the following to set some of the more exotic attributes:

    SetAttr("c:\csv.txt", System.IO.FileAttributes.Archive)

    SetAttr("c:\csv.txt", System.IO.FileAttributes.Directory)

    SetAttr("c:\csv.txt", System.IO.FileAttributes.SparseFile)

    SetAttr("c:\csv.txt", System.IO.FileAttributes.Encrypted)

    SetAttr("c:\csv.txt", System.IO.FileAttributes.Compressed)

    SetAttr("c:\csv.txt", System.IO.FileAttributes.NotContentIndexed)

    SetAttr("c:\csv.txt", System.IO.FileAttributes.Temporary)

    These attributes are rare and should be used with caution, since misuse of the attributes can cause problems for the system. Most of the entries are self-explanatory: compressed files are compressed, temporary files are meant to be temporary and quickly deleted. NotContentIndexed indicates that a file should not have its contents inspected by search utilities such as Google Desktop and Windows Search. Finally, a sparse file is an extremely large file that is usually expected to be mostly empty. Windows saves space on these files by only recording the information that has already been written to the file, then compressing the empty regions.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.