Click here to Skip to main content
15,907,492 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This code (VB.net) worked well for a long time.
After the windows 1903 update it leaves an empty file on the server.
Can someone help me?

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
              UploadFile("test.txt", Server & "/test.txt", User, PW)
    End Sub

    Public Sub UploadFile(ByVal _FileName As String, ByVal _UploadPath As String, ByVal _FTPUser As String, ByVal _FTPPass As String)

        Try
            Dim _FileInfo As New System.IO.FileInfo(_FileName)
            Dim _FtpWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(New Uri(_UploadPath)), System.Net.FtpWebRequest)
            _FtpWebRequest.Credentials = New System.Net.NetworkCredential(_FTPUser, _FTPPass)
            _FtpWebRequest.EnableSsl = True
            _FtpWebRequest.KeepAlive = False
            _FtpWebRequest.Timeout = 15000
            _FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
            _FtpWebRequest.UseBinary = True
            _FtpWebRequest.ContentLength = _FileInfo.Length
            Dim buffLength As Integer = 2048
            Dim buff(buffLength - 1) As Byte
            Dim _FileStream As System.IO.FileStream = _FileInfo.OpenRead()
            Dim _Stream As System.IO.Stream = _FtpWebRequest.GetRequestStream()
            Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength)
            Application.DoEvents()
            Do While contentLen <> 0
                _Stream.Write(buff, 0, contentLen)
                contentLen = _FileStream.Read(buff, 0, buffLength)
            Loop
                   Catch ex As Exception
        End Try
    End Sub


What I have tried:

Used framework 4.6, 4.7and 4.8
Posted
Updated 27-Dec-19 4:38am

VB
Dim buffLength As Integer = 2048
Dim buff(buffLength - 1) As Byte
Dim _FileStream As System.IO.FileStream = _FileInfo.OpenRead()
Dim _Stream As System.IO.Stream = _FtpWebRequest.GetRequestStream()
Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength)

As noted in your original message, you are creating a buffer of 2047 bytes, but reading 2048 bytes into it. This has the potential for some of your data being lost and your uploaded files consequently being corrupted.
 
Share this answer
 
Why did it work for me without being any data lost without the update to 1903?
I tried to connect the server with Total Commander, same problem...
Tried Filezilla, same problem...
Tried WINSCP: This program can connect and opload/download files as well before and after te KB4517389 is installed.
 
Share this answer
 
v2
Comments
Richard MacCutchan 27-Dec-19 11:32am    
No idea. But if you really want to know what the problem may be then you need to do some debugging to see exactly what happens when you run the program.
Brollie 27-Dec-19 11:43am    
As long as buff is longer or equal then bufferlenght - 1 the filetransfer is okay.
Brollie 27-Dec-19 11:46am    
Only if buff < bufferlength - 1 it throws an exception.
Brollie 27-Dec-19 11:48am    
After windows update to 1903 it always throws an exception at the time the stream is disposed.
Richard MacCutchan 27-Dec-19 12:11pm    
Then you need to capture the exception details to find out why.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900