file_attrib.aspx
<%@ Page Language="VB" %>
<%@ Import Namespace="System.IO" %>
<script language="VB" runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myFileInfo As FileInfo
Dim lngFileAttribs As Long
' Create a FileInfo object and point it at the file we're interested in
myFileInfo = New FileInfo(Server.MapPath("textfile.txt"))
' You shouldn't need to know these, but just in case you need to
' troubleshoot something, here's a list of the attributes and
' their values:
' Attribute Value
' ----------------------------------------
' FileAttributes.Archive = 32
' FileAttributes.Compressed = 2048
' FileAttributes.Device = 64
' FileAttributes.Directory = 16
' FileAttributes.Encrypted = 16384
' FileAttributes.Hidden = 2
' FileAttributes.Normal = 128
' FileAttributes.NotContentIndexed = 8192
' FileAttributes.Offline = 4096
' FileAttributes.ReadOnly = 1
' FileAttributes.ReparsePoint = 1024
' FileAttributes.SparseFile = 512
' FileAttributes.System = 4
' FileAttributes.Temporary = 256
' Store the value of the file's attributes to a variable for convenience
lngFileAttribs = myFileInfo.Attributes
' Display the file name
litFileName.Text = myFileInfo.Name
' The easy way to just display all a file's attributes:
litAllAttributes.Text = myFileInfo.Attributes.ToString()
' Determine and display the value of each of the attributes:
litAttribReadOnly.Text = CBool(lngFileAttribs AND FileAttributes.ReadOnly)
litAttribHidden.Text = CBool(lngFileAttribs AND FileAttributes.Hidden)
litAttribSystem.Text = CBool(lngFileAttribs AND FileAttributes.System)
litAttribDirectory.Text = CBool(lngFileAttribs AND FileAttributes.Directory)
litAttribArchive.Text = CBool(lngFileAttribs AND FileAttributes.Archive)
litAttribNormal.Text = CBool(lngFileAttribs AND FileAttributes.Normal)
litAttribTemporary.Text = CBool(lngFileAttribs AND FileAttributes.Temporary)
litAttribCompressed.Text = CBool(lngFileAttribs AND FileAttributes.Compressed)
litAttribOffline.Text = CBool(lngFileAttribs AND FileAttributes.Offline)
litAttribNotIndexed.Text = CBool(lngFileAttribs AND FileAttributes.NotContentIndexed)
litAttribEncrypted.Text = CBool(lngFileAttribs AND FileAttributes.Encrypted)
' Just like in the classic ASP version, I'm going to toggle
' the file's Archive attribute for illustration.
If (lngFileAttribs And FileAttributes.Archive) = FileAttributes.Archive Then
lngFileAttribs = lngFileAttribs - FileAttributes.Archive
litAttribArchivePre.Text = "True"
Else
lngFileAttribs = lngFileAttribs + FileAttributes.Archive
litAttribArchivePre.Text = "False"
End If
' Display what we're doing
litAttribArchivePost.Text = Not CBool(litAttribArchivePre.Text)
' Set the changes back to the FileInfo object
myFileInfo.Attributes = lngFileAttribs
' If you want to simply set some specific attributes try this syntax.
' Here I'll simply turn on the temporary file attribute of our file
' while leaving the other attributes unchanged:
'myFileInfo.Attributes = myFileInfo.Attributes OR FileAttributes.Temporary
' If you leave out the reference to the original attributes you'll
' wipe them out. For instance even if the file was read only and system
' to begin with, after the following command it'd be hidden, but would no
' longer have the read only and system attributes set.
'myFileInfo.Attributes = FileAttributes.Hidden
' Finally if you want to set multiple attributes sure you use the OR operator.
'myFileInfo.Attributes = myFileInfo.Attributes _
' OR FileAttributes.NotContentIndexed OR FileAttributes.Temporary
End Sub
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>File Attributes ASP.NET Sample</title>
</head>
<body>
<form id="myForm" runat="server">
<p>
Checking File Attributes for:
<strong><asp:Literal id="litFileName" runat="server" /></strong>
</p>
<p>
Enabled Attributes: <asp:Literal id="litAllAttributes" runat="server" />
</p>
<p>Individually:</p>
<pre>
Read Only: <asp:Literal id="litAttribReadOnly" runat="server" />
Hidden: <asp:Literal id="litAttribHidden" runat="server" />
System: <asp:Literal id="litAttribSystem" runat="server" />
Directory: <asp:Literal id="litAttribDirectory" runat="server" />
Archive: <asp:Literal id="litAttribArchive" runat="server" />
Normal: <asp:Literal id="litAttribNormal" runat="server" />
Temporary: <asp:Literal id="litAttribTemporary" runat="server" />
Compressed: <asp:Literal id="litAttribCompressed" runat="server" />
Offline: <asp:Literal id="litAttribOffline" runat="server" />
Not Indexed: <asp:Literal id="litAttribNotIndexed" runat="server" />
Encrypted: <asp:Literal id="litAttribEncrypted" runat="server" />
</pre>
<p>
The file's Archive attribute was set to
<asp:Literal id="litAttribArchivePre" runat="server" />.
This script has changed it to
<asp:Literal id="litAttribArchivePost" runat="server" />.
</p>
</form>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/file_attrib_aspx.asp">here</a>
to read about and download the source code.
</p>
</body>
</html>