Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
4.3k views
in .NET FTP by (380 points)
I am using the code below to download files from a unix ftp server. I am having a problem with the portion of the code that updates the progress bar. The way it is currently working it takes the bytes transfered and uses that as the value to update the progress bar with. The problem with that is I can't guarantee the size of each file. I tried to set the Maximum value to 999999999, but the file size (18,319KB) is to small to cause the progress bar to update if the Maximum size is that big. So what I want to be able to do is take the bytes transfered (e.ByteCount) and divide that by the total file size (18,319KB for example) then multiple the answer by 100 to get a percentage. The problem is that I am not getting an accurate file size and when I use the formula to calculate the percentage I get the following error in Visual Basic:

Operator '\' is not defined for types 'Long' and 'System.Drawing.Size'.

Can anyone help point me in the right direction? Any help will be greatly appreciated.

Thanks
Public Class Form1
    Inherits System.Windows.Forms.Form
    Delegate Sub UpdateProgressBarDelegate(ByVal byteCount As Long)
    
    Private Sub Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Connect.Click
 
        Try
            
            FtpConnection1.ServerAddress = (HRMS_Server.Text)
            FtpConnection1.UserName = (HRMS_UserName.Text)
            FtpConnection1.Password = (HRMS_Password.Text)
            FtpConnection1.Connect()
            Connect.Enabled = False
            
        Catch ex As Exception
            MsgBox("Please check your user name and/or password!", MsgBoxStyle.Critical, "Authentication Error!")
            HRMS_UserName.Clear()
            HRMS_Password.Clear()
            Connect.Enabled = True
            If FtpConnection1.IsConnected = True Then
                FtpConnection1.Close()
            End If
            GoTo notconnected
        End Try

        ' Change to Temp folder, download the users file and change the extension to .txt
        Try
            FtpConnection1.ChangeWorkingDirectory("/var/tmp")
            Dim file As String = HRMS_UserName.Text
            Dim file_1 As String = HRMS_UserName.Text & ".txt"
            Dim dlpath As String = My.Settings.folderpath
            [b]Dim Size As Int32 = FtpConnection1.GetSize(file)[/b]
            FtpConnection1.RenameFile(file, file_1)
            FtpConnection1.DownloadFile(dlpath + file_1, file_1)
            FtpConnection1.DeleteFile(file_1)
            FtpConnection1.Close()
            HRMS_UserName.Clear()
            HRMS_Password.Clear()
            Connect.Enabled = True

            ' Close the connection
        Catch ex As Exception
            MsgBox("Please check your network connection and make" & vbNewLine & "sure you have saved your file to the L1 printer!", MsgBoxStyle.Critical, "Connection/File Error!")
            FtpConnection1.DeleteOnFailure = True
            Connect.Enabled = True
            HRMS_UserName.Clear()
            HRMS_Password.Clear()
            If FtpConnection1.IsConnected = True Then
                FtpConnection1.Close()
            End If
        End Try
notconnected:
    End Sub

    Private Sub FtpConnection1_BytesTransferred(ByVal sender As Object, ByVal e As EnterpriseDT.Net.Ftp.BytesTransferredEventArgs) Handles FtpConnection1.BytesTransferred
        Dim arguments As Object
        arguments = (e.ByteCount / Size) * 100
        ProgressBar1.Invoke(New UpdateProgressBarDelegate(AddressOf UpdateProgressBar), arguments)
    End Sub

    Private Sub UpdateProgressBar(ByVal ByteCount As Long)
        ProgressBar1.Value = ByteCount
    End Sub

4 Answers

0 votes
by (51.6k points)
The Size variable you declared in the Connect_Click method is not accessible in the FtpConnection1_BytesTransferred method since it is a local variable. The Size variable you are using in FtpConnection1_BytesTransferred is the size of the form, i.e. Form.Size. Firstly, you need to make sure you get the scope of your variables right. Secondly, don't use the name, Size, since it is already used by the variable Form.Size. Use another name, such as FileSize.

Good luck.

- Hans (EDT)
0 votes
by (380 points)
I created a public variable and named it File_Size and the program now works the way I wanted it to.

Thank you so much!!!!!!!!!!

P.S.
I really appreciate all the help your support staff has given me in the past weeks. You guys respond quickly and are always helpful. If anyone needs a FTP tool I will be more than willing to send them to you guys.

Thanks again
:lol:
0 votes
by (200 points)
Hi, I need help!

I am having problem with the progress bar. I am using the free version of the edtFTPnet. The error im getting is "An Exeption has Been Thrown By Target of Invocation"

My Code:-

Delegate Sub UpdateProgressBarDelegate(ByVal byteCount As Long)

Public Sub FtpUpdate_BytesTransferred(ByVal sender As Object, ByVal e As EnterpriseDT.Net.Ftp.BytesTransferredEventArgs) Handles FtpUpdate.BytesTransferred

Dim arguments As Object
Try
arguments = (e.ByteCount / File_Size) * 100
' Error accurs when entering this invoke section'
ProgressBar1.Invoke(New UpdateProgressBarDelegate(AddressOf UpdateProgressBar), arguments)
'objPro.Refresh()
Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub

Private Sub UpdateProgressBar(ByVal ByteCount As Long)
ProgressBar1.Value = ByteCount
lblcent.Text = (ByteCount & " % ")
'objPro.Refresh()
End Sub


Thank you for any help.

Regards,

Karthik
0 votes
by (200 points)
Actually the more detail error msg is "Specified cast is not valid" in:-

ProgressBar1.Invoke(New UpdateProgressBarDelegate(AddressOf UpdateProgressBar), arguments)

Thank you.

Categories

...