Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I'm trying to understand the returned values using System.IO.File.GetAttributes(filename)
If I take a random file that shows in explorer the Boxes Checked are Read only & File Ready For Archiving
The Result If you enumerate are

File Attributes for C:\Users\Public\Wireshark IP Map a01688\ipmap.html Are :
8
2
2
5

Unchecking those 2 boxes so None are checked I get back,
File Attributes for C:\Users\Public\Wireshark IP Map a01688\ipmap.html Are :
8
1
9
2

A copy of the file with only encrypted checked returns,
File Attributes for C:\Users\Public\Wireshark IP Map a01688\ipmap - Encrypted.html Are :
2
4
5
7
6

Same Copy With encrypted,File ready for archiving ,index this file for faster searching. returns.
File Attributes for C:\Users\Public\Wireshark IP Map a01688\ipmap - Encrypted.html Are :
1
6
4
1
6

I can not find anything that defiantly says this is what this number means.

I tried using a case statement and using the Values here
http://msdn.microsoft.com/en-us/library/system.io.fileattributes(v=vs.90).aspx[^]
I assumed that it would start at 1 and go from there, but during a test of several files it returned a value of "0".
Either way if I start index at 0 or 1 results don't make sense.

The reuslts also do not appear to match up to what is defined on this page.
http://msdn.microsoft.com/en-us/library/windows/desktop/gg258117(v=vs.85).aspx[^]

Would someone please point me in the right direction.
Thank you for any help.

Code used to produce the results:
VB
Imports System
Imports System.IO
Imports System.Security.AccessControl
Imports System.Text

   Private Sub btnGetFileAttributes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetFileAttributes.Click
        Dim filename As String = Nothing
        Try

            Dim strBuilder As New StringBuilder

            Dim Attributes As String = System.IO.File.GetAttributes(filename)

            For Each atb In Attributes

                strBuilder.AppendLine(atb)
            Next

            Dim OutputString As String
            OutputString = ("File Attributes for " & filename & " Are :" & vbNewLine & strBuilder.ToString)
            TextBox1.Text = OutputString
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
Posted

You need to see at the definition of the System.IO.FileAttributes to understand this simple thing. They present a bitset:
ReadOnly = 1,<br />
Hidden = 2,<br />
System = 4,<br />
Directory = 16


To add an attribute to a bitset, you need to use binary Or operation. To check if an attribute is set, you need to perform binary And and compare the result with 0 (which means the bit is clear).

For bitwise operations, please see:
http://msdn.microsoft.com/en-us/library/wz3k228a%28v=vs.80%29.aspx[^].

See also: http://en.wikipedia.org/wiki/Bitset[^].

—SA
 
Share this answer
 
v3
Comments
ledtech3 8-Apr-12 21:33pm    
I have read the information there But, I’m trying to understand the string that is returned from that function. Something is not sinking in on the relation of the returned string and your listing for the number.

After I posted, I realized all I was doing in my code was just appending each returned character to a new line from the, System.IO.File.GetAttributes(filename).

So you are saying to decode the string that is returned you have to do a bitwise operation ?
Sergey Alexandrovich Kryukov 8-Apr-12 22:20pm    
The method ToString() already should show the bitmapped enumeration value as the set of bits like, for example "Readonly, System". This is controlled by the attribute [System.Flags].

You don't have to "decode the string"; why?! You should not present attributes as string.

You should get a habit to work with values, not string representation. You already have the bits in the value. Use them the way you want.

--SA
ledtech3 8-Apr-12 22:51pm    
The problem was I didn't know what value it was supposed represent , string, array of string,or what ever.

After reading it again am I correct in understanding that the Value returned from the GetAttributes , if i do a bitwise operation using the returned value and the Values listed in the Table , If the result is 0 then that flag is not set, if the result is 1 then the flag is set , and that file has that attribute ?
Sergey Alexandrovich Kryukov 8-Apr-12 23:00pm    
No, you should know that. It comes from the code (reflected) + documentation. Every bit is documented.

More exactly, Or operator.
Say you have the attributes value attributes.
Check (attr | FileAttributes.Hidden) > 0. True will mean the Hidden attribute is set, False -- it is not.
--SA
ledtech3 14-Apr-12 16:16pm    
The original code above only returned the first item found.
The code Listed below, the best I understand , iterates thru all of the Attribute flags and if 1 of them matches a number in out list then it returned.
The code works but is slow. Still looking for a bettter way. That code was derived from looking at the sample on the MSDN page.I'll have to rework my test case statement to see if i can just check against each number.
Thanks again for pointing me in the right direction.
Here Is what I have now:

VB
Private Sub btnGetFileAttributes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetFileAttributes.Click
        'Attribute Info From http://msdn.microsoft.com/en-us/library/system.io.fileattributes(v=vs.90).aspx

        TextBox1.Clear()
        Dim filename As String = Nothing
        Try


            If UseFileBorowser = True Then
                filename = FN
                If filename = Nothing Then
                    MsgBox("No File Was Chosen")
                    Exit Sub
                End If
            ElseIf UseFileBorowser = False Then
                filename = TextBox2.Text
                If filename = Nothing Then
                    MsgBox("No File Path Was Entered")
                    Exit Sub
                End If
            End If

            'Dim flatb As FileAttribute
            Dim strBuilder As New StringBuilder

            filename = OpenFileDialog1.FileName

            Dim FileAtri As FileAttribute   '.ToString

            For idx = 0 To 65536
                FileAtri = idx

                If idx <> System.IO.File.GetAttributes(filename) = False Then

                    'strBuilder.AppendFormat("{0,3} - {1}", idx, CType(Val(FileAtri), FlagAttr).ToString())
                    strBuilder.AppendLine()
                    strBuilder.AppendLine(CType(Val(FileAtri), FlagAttr).ToString() & vbNewLine)


                End If
            Next


            Dim OutputString As String
            OutputString = ("File Attributes for " & filename & " Are :" & vbNewLine & strBuilder.ToString)
            'OutputString = (Attributes.ToString)

            TextBox1.Text = OutputString
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


    End Sub

    <flagsattribute()> _
    Enum FlagAttr As Integer
        FILE_ATTRIBUTE_ARCHIVE = 32
        FILE_ATTRIBUTE_COMPRESSED = 2048
        FILE_ATTRIBUTE_DEVICE = 64
        FILE_ATTRIBUTE_DIRECTORY = 16
        FILE_ATTRIBUTE_ENCRYPTED = 16384
        FILE_ATTRIBUTE_HIDDEN = 2
        FILE_ATTRIBUTE_NORMAL = 128
        FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192
        FILE_ATTRIBUTE_OFFLINE = 4096
        FILE_ATTRIBUTE_READONLY = 1
        FILE_ATTRIBUTE_REPARSE_POINT = 1024
        FILE_ATTRIBUTE_SPARSE_FILE = 512
        FILE_ATTRIBUTE_SYSTEM = 4
        FILE_ATTRIBUTE_TEMPORARY = 256
        FILE_ATTRIBUTE_VIRTUAL = 65536
    End Enum


File with just Read only Checked
File Attributes for C:\Users\Public\Wireshark IP Map a01688\ipmap.html Are :

FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED

File With Just Encrypted Checked"
File Attributes for C:\Users\Public\Wireshark IP Map a01688\ipmap - Encrypted.html Are :

FILE_ATTRIBUTE_NOT_CONTENT_INDEXED, FILE_ATTRIBUTE_ENCRYPTED

Stil Does not ouput exactly the way I would Like but Appears to return the Correct Values.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900