Jump to content

Problemas ao aceder a ficheiros, com streams


Dotinho
 Share

Recommended Posts

Boa noite a todos, é o seguinte, fiz um programa para fazer backups, e para copiar ficheiros, uso a função my.filesystem.copyfile().

Só que fico chateado porque em ficheiros grandes, não me mostra a percentagem, então fiz o seguinte programinha.

Private Sub FX_CopyFile(ByVal fo As String, ByVal fd As String, _
                                Optional ByVal Ff As F_SmartSync = Nothing)
            Dim sr1 As New IO.FileStream(fo, FileMode.Open)
            Dim sr2 As New IO.FileStream(fd, FileMode.CreateNew)

            Dim buff(1024) As Byte

            While sr1.Position < sr1.Length
                sr1.Read(buff, 0, 1023)
                sr2.Write(buff, 0, 1023)

                If Ff IsNot Nothing Then
                    Ff.PA_Pb.Value = (sr1.Position / sr1.Length) * 99
                    Ff.PA_Lp.Text = Format(sr1.Position / sr1.Length, "00%")
                    Ff.S_DoEvents()
                End If
            End While

            sr1.Close()
            sr2.Close()

        End Sub

Mas não sei porque, em alguns ficheiros diz: Acesso negado ao ficheiro! (isto é uma mensagem ex.message)

Outra coisa que não sei fazer, é passar as propriedades entre o ficheiro novo e o original, do tipo gostava de passar a data de modificação, para comparar se houve alteração ou não..

Penso que seja porcausa do windows 7, mas nao sei...

Luis Lourenço

Link to comment
Share on other sites

Obrigado, mas já deu... eu mostro como dá:

Private Sub FX_CopyFile(ByVal fo As String, ByVal fd As String, _
                                Optional ByVal Ff As F_SmartSync = Nothing)
            Dim foi = New FileInfo(fo)
            Dim fdi = New FileInfo(fd)

            Dim sr1 = foi.OpenRead
            Dim sr2 = fdi.OpenWrite

            Dim buff(2048) As Byte

            While sr1.Position < sr1.Length
                sr1.Read(buff, 0, 1048)
                sr2.Write(buff, 0, 1048)

                If Ff IsNot Nothing Then
                    Ff.PA_Pb.Value = (sr1.Position / sr1.Length) * 99
                    Ff.PA_Lp.Text = Format(sr1.Position / sr1.Length, "00%")
                    Ff.S_DoEvents()
                End If
            End While

            sr1.Close()
            sr2.Close()

            fdi.LastWriteTime = foi.LastWriteTime


        End Sub

Luis Lourenço

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.