vb@rchiv
VB Classic
VB.NET
ADO.NET
VBA
C#
Blitzschnelles Erstellen von grafischen Diagrammen!  
 vb@rchiv Quick-Search: Suche startenErweiterte Suche starten   Impressum  | Datenschutz  | vb@rchiv CD Vol.6  | Shop Copyright ©2000-2024
 
zurück
Rubrik: Dateisystem · Dateien - allgemein   |   VB-Versionen: VB2005, VB2008, VB201009.01.12
Datei-Informationen abfragen

Klasse zur Abfrage von Datei-Informationen bei Pfaden, die länger sind als Max_Path.

Autor:   Manfred BohnBewertung:     [ Jetzt bewerten ]Views:  12.210 
ohne HomepageSystem:  Win7, Win8, Win10, Win11 Beispielprojekt auf CD 

Das NTFS-Dateisystem ermöglicht Pfade, die bis etwa 30000 Zeichen umfassen können.

Im Net-Framework ist bei vielen System.IO-Methoden die maximal zulässige Länge der Pfadangabe stärker beschränkt (meist 247 Zeichen für Verzeichnis-Pfade, 259 Zeichen für Datei-Pfade). Solche Methoden lösen eine "PathTooLongException" aus, "wenn ein Pfad- oder Dateiname länger als die vom System festgelegte maximale Länge ist."

Für die Abfrage von Dateisystem-Informationen gibt es Windowws-APIs, die durch Voranstellen der Zeichenfolge "\\?\" (für das lokale Dateisystem) auch Pfade verarbeiten, deren Länge diese System-Begrenzung überschreitet.

Die Klasse "cFileInfo" erlaubt die ABFRAGE von Datei-Informationen auch bei langen Pfadangaben.
Der Pfad der jeweils abzufragenden Datei ist im Konstruktor der Klasse anzugeben.

Imports System.Runtime.InteropServices
 
''' <summary>
''' Klasse zur Ermittlung von File-Informationen
''' </summary>
Public Class cFileinfo
 
#Region "Windows API: Konstanten, Strukturen und Funktionen"
  ' dwDesiredAccess Konstante
  Private Const GENERIC_READ As Integer = &H80000000 ' nur lesen
  ' dwShareMode Konstante
  Private Const FILE_SHARE_READ As Integer = &H1
  ' dwCreationDisposition Konstante
  Private Const OPEN_EXISTING As Integer = 3 ' öffnet eine bereits vorhandene Datei
  ' Rückgabe 
  Private Const INVALID_HANDLE_VALUE As Integer = -1
 
  ' File-Attributes-Konstanten
  Private Const FILE_ATTRIBUTE_DIRECTORY As Integer = &H10 'Verzeichnis
  Private Const FILE_ATTRIBUTE_ARCHIVE As Integer = &H20 ' Archiv-Datei
  Private Const FILE_ATTRIBUTE_HIDDEN As Integer = &H2 ' Versteckt
  Private Const FILE_ATTRIBUTE_NORMAL As Integer = &H80 ' Normal
  Private Const FILE_ATTRIBUTE_READONLY As Integer = &H1 ' Schreibgeschützt
  Private Const FILE_ATTRIBUTE_SYSTEM As Integer = &H4 ' Systemdatei
  Private Const FILE_ATTRIBUTE_TEMPORARY As Integer = &H100 ' temporäre Datei 
 
  <StructLayout(LayoutKind.Sequential)> _
  Private Structure FILETIME
    Dim dwLowDateTime As Integer
    Dim dwHighDateTime As Integer
  End Structure
 
  <StructLayout(LayoutKind.Sequential)> _
  Private Structure SYSTEMTIME
    Dim wYear As Short
    Dim wMonth As Short
    Dim wDayOfWeek As Short
    Dim wDay As Short
    Dim wHour As Short
    Dim wMinute As Short
    Dim wSecond As Short
    Dim wMilliseconds As Short
  End Structure
 
  <StructLayout(LayoutKind.Sequential)> _
      Private Structure WIN32_FILE_ATTRIBUTE_DATA
    Public dwFileAttributes As Integer
    Public ftCreationTime As FILETIME
    Public ftLastAccessTime As FILETIME
    Public ftLastWriteTime As FILETIME
    Public nFileSizeHigh As UInt32
    Public nFileSizeLow As UInt32
  End Structure
 
  <DllImport("kernel32.dll", CharSet:=CharSet.Auto, _
  CallingConvention:=CallingConvention.StdCall)> _
  Private Shared Function CreateFile( _
  ByVal lpFileName As String, _
  ByVal dwDesiredAccess As Integer, _
  ByVal dwShareMode As Integer, _
  ByVal SecurityAttributes As System.IntPtr, _
  ByVal dwCreationDisposition As UInteger, _
  ByVal dwFlagsAndAttributes As UInteger, _
  ByVal hTemplateFile As System.IntPtr) As System.IntPtr
  End Function
 
  <DllImport("kernel32.dll")> _
  Private Shared Function CloseHandle( _
  ByVal handle As System.IntPtr) As Boolean
  End Function
 
  <DllImport("kernel32.dll", CharSet:=CharSet.Auto, Setlasterror:=True)> _
  Private Shared Function GetFileSizeEx(ByVal hFile As System.IntPtr, _
    ByRef lpFileSize As Long) As Boolean
  End Function
 
  <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
  Private Shared Function GetFileAttributesEx( _
    ByVal lpFilename As String, _
    ByVal fileInfoLevel As Integer, _
    ByRef lpFileInformation As WIN32_FILE_ATTRIBUTE_DATA) As Boolean
  End Function
 
  <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
  Private Shared Function GetFileTime(ByVal hFile As System.IntPtr, _
    ByRef lpCreationTime As FILETIME, _
    ByRef lpLastAccessTime As FILETIME, _
    ByRef lpLastWriteTime As FILETIME) As Boolean
  End Function
 
  <DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _
  Private Shared Function FileTimeToSystemTime(ByRef lpFileTime As FILETIME, _
    ByRef lpSystemTime As SYSTEMTIME) As Boolean
  End Function
 
#End Region
 
 
#Region "Member und Properties"
 
  ' ================================
  ' Membervariablen
  ' ================================
 
  Dim _FullPath As String
  Dim _CTimeUTC, _AtimeUTC, _WTimeUTC As Date
  Dim _FileSize As Long
  Dim _Archive, _Hidden, _ReadOnly, _System As Boolean
  Dim _Attributes As Integer
 
  ' =================================
  ' Properties
  ' =================================
  ''' <summary>
  ''' Ruft den vollständigen Pfad der Datei ab
  ''' </summary>
  Public ReadOnly Property FullName() As String
    Get
      Return (_FullPath)
    End Get
  End Property
 
  ''' <summary>Ruft den Namen der Datei ab</summary>
  Public ReadOnly Property Name() As String
    Get
      Dim parts() As String = _FullPath.Split _
      (IO.Path.DirectorySeparatorChar, IO.Path.AltDirectorySeparatorChar)
      Return parts(parts.GetUpperBound(0))
    End Get
  End Property
 
  ''' <summary>
  ''' Ruft eine Zeichenfolge ab, die den Erweiterungsteil der Datei darstellt.
  ''' </summary>
  Public ReadOnly Property Extension() As String
    Get
      Dim nam As String = Name
      Dim parts() As String = _FullPath.Split("."c)
      If parts.Length = 1 Then Return String.Empty
      Return "." & parts(parts.GetUpperBound(0))
    End Get
  End Property
 
  ''' <summary>
  ''' Ruft eine Zeichenfolge ab, die den vollständigen Pfad des 
  ''' Verzeichnisses darstellt.
  ''' </summary>
  Public ReadOnly Property DirectoryName() As String
    Get
      Dim parts() As String = _FullPath.Split _
      (IO.Path.DirectorySeparatorChar, IO.Path.AltDirectorySeparatorChar)
      Array.Resize(parts, parts.Length - 1)
      Return String.Join(IO.Path.DirectorySeparatorChar, parts)
    End Get
  End Property
 
  ''' <summary>
  ''' Ruft den Erstellungs-Zeitpunkt der aktuellen Datei ab 
  ''' (Coordinated  Universal Time)
  ''' </summary>
  Public ReadOnly Property CreationTimeUTC() As Date
    Get
      Return _CTimeUTC
    End Get
  End Property
 
  ''' <summary>
  ''' Ruft den Zeitpunkt des letzten Schreibens in die Datei ab 
  ''' (Coordinated  Universal Time)
  ''' </summary>
  Public ReadOnly Property LastWriteTimeUTC() As Date
    Get
      Return _WTimeUTC
    End Get
  End Property
 
  ''' <summary>
  ''' Ruft den Zeitpunkt des letzten Zugriffs auf die aktuelle 
  ''' Datei ab (Coordinated  Universal Time)
  ''' </summary>
  Public ReadOnly Property LastAccessTimeUTC() As Date
    Get
      Return _AtimeUTC
    End Get
  End Property
 
  ''' <summary>Ruft die Größe der aktuellen Datei in Byte ab</summary>
  Public ReadOnly Property Length() As Long
    Get
      Return _FileSize
    End Get
  End Property
 
  ''' <summary>
  ''' Ruft die Attribute für die aktuelle Datei ab
  ''' </summary>
  Public ReadOnly Property Attributes() As Integer
    Get
      Return _Attributes
    End Get
  End Property
 
  ''' <summary>
  ''' Abfrage: Ist für die aktuelle Datei das Archive-Flag gesetzt? 
  ''' </summary>
  Public ReadOnly Property IsArchive() As Boolean
    Get
      If _Attributes < 0 Then Throw New NotSupportedException
      Return _Archive
    End Get
  End Property
 
  ''' <summary>
  ''' Abfrage: Ist die aktuelle Datei als "versteckt" gekennzeichnet?
  ''' </summary>
  Public ReadOnly Property IsHidden() As Boolean
    Get
      If _Attributes < 0 Then Throw New NotSupportedException
      Return _Hidden
    End Get
  End Property
 
  ''' <summary>Abfrage: Ist die aktuelle Datei schreibgeschützt?</summary>
  Public ReadOnly Property IsReadOnly() As Boolean
    Get
      If _Attributes < 0 Then Throw New NotSupportedException
      Return _ReadOnly
    End Get
  End Property
 
  ''' <summary>
  ''' Abfrage: Ist die aktuelle Datei als Systemdatei gekennzeichnet?
  ''' </summary>
  Public ReadOnly Property IsSystem() As Boolean
    Get
      If _Attributes < 0 Then Throw New NotSupportedException
      Return _System
    End Get
  End Property
#End Region
 
#Region "Konstruktor: Abfrage der Informationen"
 
  ''' <summary>
  ''' Initialisiert eine neue Instanz der cFileklasse
  ''' (zur Abfrage von Datei-Informationen)
  ''' </summary>
  ''' <param name="FullPath">Der voll-qualifizierte Name der Datei</param>
  Public Sub New(ByVal FullPath As String)
 
    Dim hFile As System.IntPtr
    Dim CTime, ATime, WTime As FILETIME
 
    _FullPath = FullPath
 
    ' Lange Pfadangaben ermöglichen
    FullPath = "\\?\" & FullPath
 
    ' Datei-Handle besorgen
    hFile = CreateFile(FullPath, GENERIC_READ, FILE_SHARE_READ, IntPtr.Zero, _
      OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, IntPtr.Zero)
 
    If hFile.ToInt32 = INVALID_HANDLE_VALUE Then Throw New IO.FileNotFoundException
 
    If Not GetFileTime(hFile, CTime, ATime, WTime) Then Throw New InvalidOperationException
    If Not GetFileSizeEx(hFile, _FileSize) Then Throw New InvalidOperationException
    CloseHandle(hFile)
 
    _CTimeUTC = ToDateTime(CTime)
    _AtimeUTC = ToDateTime(ATime)
    _WTimeUTC = ToDateTime(WTime)
 
    Dim fileattr As WIN32_FILE_ATTRIBUTE_DATA
    If Not GetFileAttributesEx(FullPath, 0, fileattr) Then
      Throw New InvalidOperationException
    End If
    _Attributes = fileattr.dwFileAttributes
    _Hidden = (FILE_ATTRIBUTE_HIDDEN = (_Attributes And FILE_ATTRIBUTE_HIDDEN))
    _System = (FILE_ATTRIBUTE_SYSTEM = (_Attributes And FILE_ATTRIBUTE_SYSTEM))
    _ReadOnly = (FILE_ATTRIBUTE_READONLY = (_Attributes And FILE_ATTRIBUTE_READONLY))
    _Archive = (FILE_ATTRIBUTE_ARCHIVE = (_Attributes And FILE_ATTRIBUTE_ARCHIVE))
  End Sub
 
  Private Function ToDateTime(ByVal ftim As FILETIME) As Date
    Dim stim As SYSTEMTIME
    FileTimeToSystemTime(ftim, stim)
    With stim
      Return New Date(.wYear, .wMonth, .wDay, .wHour, .wMinute, .wSecond, .wMilliseconds)
    End With
  End Function
#End Region
End Class

Dieser Tipp wurde bereits 12.210 mal aufgerufen.

Voriger Tipp   |   Zufälliger Tipp   |   Nächster Tipp

Über diesen Tipp im Forum diskutieren
Haben Sie Fragen oder Anregungen zu diesem Tipp, können Sie gerne mit anderen darüber in unserem Forum diskutieren.

Neue Diskussion eröffnen

nach obenzurück


Anzeige

Kauftipp Unser Dauerbrenner!Diesen und auch alle anderen Tipps & Tricks finden Sie auch auf unserer aktuellen vb@rchiv  Vol.6
(einschl. Beispielprojekt!)

Ein absolutes Muss - Geballtes Wissen aus mehr als 8 Jahren vb@rchiv!
- nahezu alle Tipps & Tricks und Workshops mit Beispielprojekten
- Symbol-Galerie mit mehr als 3.200 Icons im modernen Look
Weitere Infos - 4 Entwickler-Vollversionen (u.a. sevFTP für .NET), Online-Update-Funktion u.v.m.
 
   

Druckansicht Druckansicht Copyright ©2000-2024 vb@rchiv Dieter Otter
Alle Rechte vorbehalten.
Microsoft, Windows und Visual Basic sind entweder eingetragene Marken oder Marken der Microsoft Corporation in den USA und/oder anderen Ländern. Weitere auf dieser Homepage aufgeführten Produkt- und Firmennamen können geschützte Marken ihrer jeweiligen Inhaber sein.

Diese Seiten wurden optimiert für eine Bildschirmauflösung von mind. 1280x1024 Pixel