VB.Net by API/System.IO/FileAttributes

Материал из VB Эксперт
Версия от 12:50, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

FileAttributes.Hidden

  
Imports System
Imports System.IO
Public Class MainClass
  Shared Sub Main()
   Dim myDirectory As DirectoryInfo
    myDirectory = New DirectoryInfo("C:\")
    Dim aFile As FileInfo
    For Each aFile In myDirectory.GetFiles
        If (aFile.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden Then
            Console.WriteLine("FOUND hidden file named " & aFile.FullName)
        End If        
    Next
   End Sub
  
End Class


FileAttributes.Normal

  
Option Strict On
Public Module modErr
   Public Sub Main()
    Dim filename As String
    filename = Dir("*.*", FileAttribute.Normal)
    Do While Not filename = String.Empty
       Console.WriteLine(filename)
       filename = Dir()
    Loop
   End Sub
End Module


FileAttributes.ReadOnly

  
Imports System.IO
Module Module1
    Sub ProcessTree(ByVal Dir As String)
        Dim DirObj As New DirectoryInfo(Dir)
        Dim Files As FileInfo() = DirObj.GetFiles("*.*")
        Dim Dirs As DirectoryInfo() = DirObj.GetDirectories("*.*")
 
        Dim Filename As FileInfo
        For Each Filename In Files
            Try
                If (Filename.Attributes And FileAttributes.ReadOnly) Then
                    Filename.Attributes = (Filename.Attributes And Not FileAttributes.ReadOnly)
                End If
            Catch E As Exception
                Console.WriteLine("Error changing attribute for {0}", Filename.FullName)
                Console.WriteLine("Error: {0}", E.Message)
            End Try
        Next
        Dim DirectoryName As DirectoryInfo
        For Each DirectoryName In Dirs
            Try
                ProcessTree(DirectoryName.FullName)
            Catch E As Exception
                Console.WriteLine("Error accessing {0}", DirectoryName.FullName)
                Console.WriteLine("Error: {0}", E.Message)
            End Try
        Next
    End Sub
    Sub Main()
        ProcessTree("C:\")
    End Sub
End Module


FileAttributes.Temporary

  
public class Test
   public Shared Sub Main
        Dim lngAttributes As Long
        lngAttributes = System.IO.File.GetAttributes("test.txt")
        " Use a binary AND to extract the specific attributes.
        Console.WriteLine("Normal: " & CBool(lngAttributes And IO.FileAttributes.Normal))
        Console.WriteLine("Hidden: " & CBool(lngAttributes And IO.FileAttributes.Hidden))
        Console.WriteLine("ReadOnly: " & CBool(lngAttributes And IO.FileAttributes.ReadOnly))
        Console.WriteLine("System: " & CBool(lngAttributes And IO.FileAttributes.System))
        Console.WriteLine("Temporary File: " & CBool(lngAttributes And IO.FileAttributes.Temporary))
        Console.WriteLine("Archive: " & CBool(lngAttributes And IO.FileAttributes.Archive))
   End Sub
End Class