none
暗号化ファイル(フォルダ)の代替データストリーム取得 RRS feed

  • 質問

  • みなさん、

    お疲れ様です。下記のコードで属性なしとH属性付けてファイルの代替データストリーム取得できましたがE属性確認付けてファイルの代替データストリーム取得できないのでどうやって実装すれば良いでしょうか。教えていただけますでしょうか。ありがとうございます。

    Imports System
    Imports System.Collections.Generic
    Imports System.Runtime.InteropServices
    Imports System.ComponentModel
    Imports Microsoft.Win32.SafeHandles
    Imports System.IO
    Public Class Class1
        Shared Sub Main(ByVal args As String())
            For Each path As String In args
                Console.WriteLine(path & ":")
                For Each stream As StreamInfo In FileStreamSearcher.GetStreams(New FileInfo(path))
                    If stream.Type.ToString() = "AlternateData" Then
                        Console.WriteLine(stream.Name & "|" & stream.Size & "|" & stream.Type)
                    End If
                Next
            Next
            Console.ReadKey()
        End Sub

        Public Enum StreamType
            Data = 1
            ExternalData = 2
            SecurityData = 3
            AlternateData = 4
            Link = 5
            PropertyData = 6
            ObjectID = 7
            ReparseData = 8
            SparseDock = 9
        End Enum

        Public Structure StreamInfo
            Public Sub New(ByVal name As String, ByVal type As StreamType, ByVal size As Long)
                Me.Name = name
                Me.Type = type
                Me.Size = size
            End Sub

            Public ReadOnly Name As String
            Public ReadOnly Type As StreamType
            Public ReadOnly Size As Long
        End Structure

        Public Class FileStreamSearcher
            <DllImport("kernel32.dll")>
            Private Shared Function BackupRead(ByVal hFile As SafeFileHandle, ByVal lpBuffer As IntPtr, ByVal nNumberOfBytesToRead As UInteger, <Out> ByRef lpNumberOfBytesRead As UInteger, <MarshalAs(UnmanagedType.Bool)> ByVal bAbort As Boolean, <MarshalAs(UnmanagedType.Bool)> ByVal bProcessSecurity As Boolean, ByRef lpContext As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
            End Function

            <DllImport("kernel32.dll")>
            Private Shared Function BackupSeek(ByVal hFile As SafeFileHandle, ByVal dwLowBytesToSeek As UInteger, ByVal dwHighBytesToSeek As UInteger, <Out> ByRef lpdwLowByteSeeked As UInteger, <Out> ByRef lpdwHighByteSeeked As UInteger, ByRef lpContext As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
            End Function

            Public Shared Iterator Function GetStreams(ByVal file As FileInfo) As IEnumerable(Of StreamInfo)
                Const bufferSize As Integer = 4096

                Using fs As FileStream = file.OpenRead()
                    Dim context As IntPtr = IntPtr.Zero
                    Dim buffer As IntPtr = Marshal.AllocHGlobal(bufferSize)

                    Try

                        While True
                            Dim numRead As UInteger
                            If Not BackupRead(fs.SafeFileHandle, buffer, CUInt(Marshal.SizeOf(GetType(Win32StreamID))), numRead, False, True, context) Then Throw New Win32Exception()

                            If numRead > 0 Then
                                Dim streamID As Win32StreamID = CType(Marshal.PtrToStructure(buffer, GetType(Win32StreamID)), Win32StreamID)
                                Dim attr As String = streamID.dwStreamAttributes
                                Dim name As String = Nothing

                                If streamID.dwStreamNameSize > 0 Then
                                    If Not BackupRead(fs.SafeFileHandle, buffer, CUInt(Math.Min(bufferSize, streamID.dwStreamNameSize)), numRead, False, True, context) Then Throw New Win32Exception()
                                    name = Marshal.PtrToStringUni(buffer, CInt(numRead) / 2)
                                End If

                                Yield New StreamInfo(name, streamID.dwStreamId, streamID.Size)

                                If streamID.Size > 0 Then
                                    Dim lo, hi As UInteger
                                    BackupSeek(fs.SafeFileHandle, UInteger.MaxValue, Integer.MaxValue, lo, hi, context)
                                End If
                            Else
                                Exit While
                            End If
                        End While

                    Finally
                        Marshal.FreeHGlobal(buffer)
                        Dim numRead As UInteger
                        If Not BackupRead(fs.SafeFileHandle, IntPtr.Zero, 0, numRead, True, False, context) Then Throw New Win32Exception()
                    End Try
                End Using
            End Function
        End Class

        <StructLayout(LayoutKind.Sequential, Pack:=4)>
        Public Structure Win32StreamID
            Public dwStreamId As StreamType
            Public dwStreamAttributes As Integer
            Public Size As Long
            Public dwStreamNameSize As Integer
        End Structure

    End Class


    2019年6月19日 5:35

回答

  • BackupReadには

    This function is not intended for use in backing up files encrypted under the Encrypted File System. Use ReadEncryptedFileRaw for that purpose.

    と書かれている通りだと思います。

    • 回答としてマーク Luky9 2019年6月20日 0:58
    2019年6月19日 6:26

すべての返信

  • BackupReadには

    This function is not intended for use in backing up files encrypted under the Encrypted File System. Use ReadEncryptedFileRaw for that purpose.

    と書かれている通りだと思います。

    • 回答としてマーク Luky9 2019年6月20日 0:58
    2019年6月19日 6:26
  • BackupReadには

    This function is not intended for use in backing up files encrypted under the Encrypted File System. Use ReadEncryptedFileRaw for that purpose.

    と書かれている通りだと思います。

    佐祐理さん、

    ReadEncryptedFileRawを使ってことがありますか。参考になるものあったら宜しくお願い致します。

    2019年6月19日 6:31