65.9K
CodeProject 正在变化。 阅读更多。
Home

文件夹编码器

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.90/5 (6投票s)

2007 年 12 月 11 日

CPOL
viewsIcon

42883

downloadIcon

574

一种将文件夹转换为文件的方法...

开始...

好吧,这都怪我妹妹... 她说如果我给她做一个锁定文件夹的应用程序,她就给我一个“独立”的标志... 我还能怎么办呢...?

代码

基本上,这是我用来编码所选文件夹的代码...

Private Sub Code()
    Try
        'prepares the writer to write
        Dim fs As New FileStream("C:\Mitio\Locked.log",
            FileMode.Append)
        Dim sw As New StreamWriter(fs)
        ' sets the progressbar max value
        ProgressBar1.Maximum = lb.Items.Count

        For Each obj As Object In lb.Items
            Dim name As String = My.Computer.FileSystem.GetName(
                obj.ToString)
            Dim bytes() As Byte
            ' reads the bytes from the specified fie & stores them in
            ' the array
            bytes = My.Computer.FileSystem.ReadAllBytes(obj.ToString)

            Dim bn As Integer = bytes.Length ' gets the length of the bytes
            ' appends the bytes of the array to the newly created
            ' Locked.locked file
            My.Computer.FileSystem.WriteAllBytes(
                "C:\Mitio\Locked.Locked", bytes, True) 

            'writes the name of the file and the length of the
            'binaries in it
            sw.WriteLine(bn.ToString + "*" + name) 

            ProgressBar1.Value += 1 ' performs a step

        Next
        lb.Items.Clear() 'clears the list

        sw.Close() 'closes the writer

        fs.Close() 'and the file

        ProgressBar1.Value = 0 ' resets the  progressbar

    Catch ex As Exception
        ' if anything happens, you will be gently informed :D
        MsgBox(ex.Message)

    End Try
End Sub

我想没什么特别的...

这是解码文件夹的代码

Private sub Decode()
    Try
        Dim bf As New FileStream("C:\Mitio\Locked.locked", 
            FileMode.Open, FileAccess.Read, FileShare.Inheritable)
        Dim br As BinaryReader ' opens the LOCKED file

        Dim sf As New FileStream("C:\Mitio\Locked.log", 
            FileMode.Open)
        Dim sr As New StreamReader(sf) ' Opens the LOG file

        Dim s As String = sr.ReadLine
        lb.Items.Add(CObj(s))
        While Not s Is "" ' adds all files from the LOG file

            s = sr.ReadLine
            If s = "" Then Exit While
            lb.Items.Add(CObj(s))
        End While
        Label1.Text = lb.Items.Count
        sr.Close() ' closes the LOG file

        sf.Close()
        Dim j As Integer = 0
        br = New BinaryReader(bf)
        Dim i As Integer = 0
        pb2.Maximum = Label1.Text
        For Each obj As Object In lb.Items
            pb2.Value += 1 ' performs step

            Dim ss() As String = obj.ToString.Split("*") 'spit 

            Dim bytes As Integer = ss(0) 'the binaries

            Dim name As String = (ss(1)) 'and the filename 
            'creates a new file with the name from the list
            Dim bf1 As New FileStream(txtOut.Text + name, FileMode.Append, 
                FileAccess.Write, FileShare.Write)

            Dim bw As New BinaryWriter(bf1)
            Try
                For i = 1 To CInt(bytes)
                    'writes the number of bytes for each file
                    bw.Write(br.ReadByte()) 

                Next
            Catch ex As Exception
                MsgBox(ex.Message) ' again if something happens...

            End Try
            bw.Close() 'close...

            bf1.Close()
        Next
        br.Close() 'and close again

        bf.Close()
        lb.Items.Clear() 'clears the list again

        pb2.Value = 0 ' resets the progressbar

    Catch ex As Exception
        MsgBox(ex.Message) 'ohh

    End Try
End Sub

毕竟

它还没有完全完成,但我会保持联系...

© . All rights reserved.