none
add a file attribute to existing attributes RRS feed

  • Question

  • I know how to set an attribute and set multiple attributes but how would I add a attribute to the existing attributes... 
    Tuesday, June 16, 2009 6:29 PM

Answers

  • i just put this together, how to set and remove attribute
    Public Sub att()
            Dim fl As New FileInfo("C:\bk.txt")
            'set individual attributes
            fl.Attributes = fl.Attributes Or IO.FileAttributes.ReadOnly
            fl.Attributes = fl.Attributes Or IO.FileAttributes.Hidden
            fl.Attributes = fl.Attributes Or IO.FileAttributes.System
            fl.Attributes = fl.Attributes Or IO.FileAttributes.Archive
    
            'remove individual attribute 
            fl.Attributes = fl.Attributes And Not IO.FileAttributes.System
            fl.Attributes = fl.Attributes And Not IO.FileAttributes.Hidden
            fl.Attributes = fl.Attributes And Not IO.FileAttributes.ReadOnly
            fl.Attributes = fl.Attributes And Not IO.FileAttributes.Archive
    
            'check for individual attribute
            'Example
            If (fl.Attributes And FileAttributes.System) = FileAttributes.System Then
                ' this work, because it check for system attributes in the list of file attributes
            End If
            If fl.Attributes = FileAttributes.System Then
                ' this will fail if more attributes are set
            End If
        End Sub
    kaymaf
    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    • Proposed as answer by bdbodger Tuesday, June 16, 2009 9:50 PM
    • Unproposed as answer by Dmitry Post Tuesday, June 16, 2009 9:56 PM
    • Marked as answer by Dmitry Post Tuesday, June 16, 2009 10:41 PM
    Tuesday, June 16, 2009 9:42 PM
  • the or adds both if you use the right syntax . In Kaymaf's example if he left out fl.Attributes in the second part of this statement

     fl.Attributes = fl.Attributes Or IO.FileAttributes.ReadOnly

    for example

     fl.Attributes =  IO.FileAttributes.ReadOnly

    then  IO.FileAttributes.ReadOnly would be the only attribute
    Coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    • Marked as answer by Dmitry Post Tuesday, June 16, 2009 10:41 PM
    Tuesday, June 16, 2009 9:48 PM
  • No it didn't work exactly... but i did figure it out with the way that was shown....
    I used this code...
    Dim checkFile As system.IO.FileInfo
    checkFile = My.Computer.FileSystem.GetFileInfo(Textbox1.Text)
    checkFile.Attributes = checkFile.Attributes OR IO.FileAttributes.Hidden
    Its similar but not exactly thanks alot... this has made my day. :) Thanks to all.
    • Marked as answer by Dmitry Post Tuesday, June 16, 2009 10:40 PM
    Tuesday, June 16, 2009 10:40 PM

All replies

  • like I said I know how to set attributes but what I want is to be able to add attribute(s) to the existing attributes like a file has the attribute archive and hidden I want to be able to add any other attribute but also keep the originals. I know how to set multiple attributes but that way I have to make hundreds of If statements that would combine all possible combinations of attributes. So I want it to read the current attributes and just add attribute(s) no matter what they are... unless they contradict each other.
    Tuesday, June 16, 2009 6:42 PM
  • I want to be able to add the hidden attribute to any existing attribute without knowing what it is... so your example assumes that you know the attributes. Is there a way to be able to read the attribute then add the ones that I want with no assumptions of what the current ones are.
    Tuesday, June 16, 2009 7:36 PM
  • Here is another example of adding a new attribute to existing attributes .

    Imports System.IO.File
    
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            ' Shows normal attribute
            Dim Attributes As System.IO.FileAttributes = GetAttributes("c:\test.txt")
            MsgBox(GetAttributes("c:\test.txt").ToString)
    
            ' Shows Archive attribute
            System.IO.File.SetAttributes("c:\test.txt", GetAttributes("c:\test.txt") Or IO.FileAttributes.Archive)
            MsgBox(GetAttributes("c:\test.txt").ToString)
    
            ' Shows Archive,Hidden attributes
            System.IO.File.SetAttributes("c:\test.txt", GetAttributes("c:\test.txt") Or IO.FileAttributes.Hidden)
            MsgBox(GetAttributes("c:\test.txt").ToString)
    
            ' Shows normal attribute
            System.IO.File.SetAttributes("c:\test.txt", IO.FileAttributes.Normal)
            MsgBox(GetAttributes("c:\test.txt").ToString)
    
        End Sub
    
    End Class
    



    Coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Tuesday, June 16, 2009 9:05 PM
  • i know this will set attribute to readonly and hidden
    Dim fl As New FileInfo("C:\bk.txt")
    fl.IsReadOnly =
    True
    fl.Attributes = fl.Attributes Or IO.FileAttributes.Hidden

    Also, read more about attributes at http://msdn.microsoft.com/en-us/library/system.io.filesysteminfo.attributes.aspx

    kaymaf

    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    Tuesday, June 16, 2009 9:06 PM
  • Hey I have made a way to do so by using the number form of the attributes...
    1. get the nember form of the attribute
    2. then add the number form of attribute of the ones that I want to add

    so how would I get the number form of the attribute and dose adding the number forms together make the attributes combined?
    Tuesday, June 16, 2009 9:21 PM
  • Here is another example of adding a new attribute to existing attributes .

    Imports System.IO.File
    
    
    
    
    
    Public Class Form1
    
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    
    
            ' Shows normal attribute
    
            Dim Attributes As System.IO.FileAttributes = GetAttributes("c:\test.txt")
    
            MsgBox(GetAttributes("c:\test.txt").ToString)
    
    
    
            ' Shows Archive attribute
    
            System.IO.File.SetAttributes("c:\test.txt", GetAttributes("c:\test.txt") Or IO.FileAttributes.Archive)
    
            MsgBox(GetAttributes("c:\test.txt").ToString)
    
    
    
            ' Shows Archive,Hidden attributes
    
            System.IO.File.SetAttributes("c:\test.txt", GetAttributes("c:\test.txt") Or IO.FileAttributes.Hidden)
    
            MsgBox(GetAttributes("c:\test.txt").ToString)
    
    
    
            ' Shows normal attribute
    
            System.IO.File.SetAttributes("c:\test.txt", IO.FileAttributes.Normal)
    
            MsgBox(GetAttributes("c:\test.txt").ToString)
    
    
    
        End Sub
    
    
    
    End Class
    
    
    
    



    Coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful

    Hey dose the or part just add the attibute specified to the current ones?
    Tuesday, June 16, 2009 9:23 PM
  • i just put this together, how to set and remove attribute
    Public Sub att()
            Dim fl As New FileInfo("C:\bk.txt")
            'set individual attributes
            fl.Attributes = fl.Attributes Or IO.FileAttributes.ReadOnly
            fl.Attributes = fl.Attributes Or IO.FileAttributes.Hidden
            fl.Attributes = fl.Attributes Or IO.FileAttributes.System
            fl.Attributes = fl.Attributes Or IO.FileAttributes.Archive
    
            'remove individual attribute 
            fl.Attributes = fl.Attributes And Not IO.FileAttributes.System
            fl.Attributes = fl.Attributes And Not IO.FileAttributes.Hidden
            fl.Attributes = fl.Attributes And Not IO.FileAttributes.ReadOnly
            fl.Attributes = fl.Attributes And Not IO.FileAttributes.Archive
    
            'check for individual attribute
            'Example
            If (fl.Attributes And FileAttributes.System) = FileAttributes.System Then
                ' this work, because it check for system attributes in the list of file attributes
            End If
            If fl.Attributes = FileAttributes.System Then
                ' this will fail if more attributes are set
            End If
        End Sub
    kaymaf
    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    • Proposed as answer by bdbodger Tuesday, June 16, 2009 9:50 PM
    • Unproposed as answer by Dmitry Post Tuesday, June 16, 2009 9:56 PM
    • Marked as answer by Dmitry Post Tuesday, June 16, 2009 10:41 PM
    Tuesday, June 16, 2009 9:42 PM
  • the or adds both if you use the right syntax . In Kaymaf's example if he left out fl.Attributes in the second part of this statement

     fl.Attributes = fl.Attributes Or IO.FileAttributes.ReadOnly

    for example

     fl.Attributes =  IO.FileAttributes.ReadOnly

    then  IO.FileAttributes.ReadOnly would be the only attribute
    Coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    • Marked as answer by Dmitry Post Tuesday, June 16, 2009 10:41 PM
    Tuesday, June 16, 2009 9:48 PM
  • the or adds both if you use the right syntax . In Kaymaf's example if he left out fl.Attributes in the second part of this statement

     fl.Attributes = fl.Attributes Or IO.FileAttributes.ReadOnly

    for example

     fl.Attributes =  IO.FileAttributes.ReadOnly

    then  IO.FileAttributes.ReadOnly would be the only attribute
    Coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful

    so fl.attributes = fl.attributes or io.fileattributes.readonly
    add the readonly attruibute to what ever attibute that already exists?
    Tuesday, June 16, 2009 9:53 PM
  • I know how to set an attribute and set multiple attributes but how would I add a attribute to the existing attributes... 

    @ Dmitry, Is it not what you want?, you want to add more attribute to existing attribute(s). tell me if is not not working
    kaymaf
    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    Tuesday, June 16, 2009 10:00 PM
  • No it didn't work exactly... but i did figure it out with the way that was shown....
    I used this code...
    Dim checkFile As system.IO.FileInfo
    checkFile = My.Computer.FileSystem.GetFileInfo(Textbox1.Text)
    checkFile.Attributes = checkFile.Attributes OR IO.FileAttributes.Hidden
    Its similar but not exactly thanks alot... this has made my day. :) Thanks to all.
    • Marked as answer by Dmitry Post Tuesday, June 16, 2009 10:40 PM
    Tuesday, June 16, 2009 10:40 PM