0

This is very specific. I need help with using a C# function in my my VB.Net program The function is in a DLL and the code is below:

public void UploadData(string FTPUri, string FilePath, string FileName,
                       string UserName, string Password)
{
    StatusUp = new Int64[2];

    reqFTP = (FtpWebRequest)FtpWebRequest.Create(FTPUri + FileName);
    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
    reqFTP.UseBinary = true;
    reqFTP.Credentials = new NetworkCredential(UserName, Password);

    FileInfo fileInf = new FileInfo(FilePath);

    FileStream fs = fileInf.OpenRead();

    // modifyied code
    int bytesSize = 0;
    byte[] UpBuffer = new byte[2048];

    ftpStream = reqFTP.GetRequestStream();

    bytesSize = fs.Read(UpBuffer, 0, UpBuffer.Length);

    while ((bytesSize = fs.Read(UpBuffer, 0, UpBuffer.Length)) > 0)
    {
        StatusUp[0] = StatusUp[0] + UpBuffer.Length;
        StatusUp[1] = fileInf.Length;// +startPointInt;

        ftpStream.Write(UpBuffer, 0, bytesSize);
    }

    fs.Close();
    ftpStream.Close();
}

In my vb.net program I am calling it like this:

Dim FtpUpload As FTPUtility.ftpUtility = New FTPUtility.ftpUtility
FtpUpload.UploadData("ftp://ftp.xxx.xx", "C:\winzip.log", "/winzip.log", "uploader", "xxxx")

It works ok except it is 2 bytes short when it is done. I don't know enough C# to figure out if the C# code is wrong (I didn't write it and the guy who did has left the company), but somewhere it appears that it is not closing the file or something.

Any ideas?

4
  • 1
    Apparently you also don't know enough C# to tell it apart from C++ ;-] Commented Jan 1, 2012 at 14:00
  • Looks correct to me. Is it always 2 bytes short? Does it happen for every file? Commented Jan 1, 2012 at 14:55
  • Haha! :-) Yes, now I do look incredibly stupid. You are right, it is C# - I just phased out since I had been looking at the same guy's C++ code all day. Anyway, thanx! Commented Jan 1, 2012 at 16:19
  • 1
    Sorry, another phase out. It is always 2KB short (not 2 bytes) and the buffer in the code is 2048 so there is some connection there. I also tried a 1K file it showed 0K and was blank inside. The other files are missing the top few lines, so it appears that the first chunk is not being written. Commented Jan 1, 2012 at 16:25

3 Answers 3

1
bytesSize = fs.Read(UpBuffer, 0, UpBuffer.Length);

while ((bytesSize = fs.Read(UpBuffer, 0, UpBuffer.Length)) > 0)
{
    StatusUp[0] = StatusUp[0] + UpBuffer.Length;
    // etc..
}

Two red flags here. The first one is the cause of your problem, the extra fs.Read() call before you enter the while loop. That's 2048 bytes you don't use and don't upload. Just delete that line. You avoid these kind of bugs by using for (;;) and break.

The StatusUp[0] assignment looks bad, you should add bytesSize, not UpBuffer.Length. It isn't otherwise obvious what side-effects that has.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanx! That looks like it! I will have to try it tomorrow and will let you know.
BTW, any idea how I would use the StatusUp in the above to show a percentage or a progress bar? Thanx!
That was the right guess then. ProgressBar.Value = (int)(100L * StatusUp[0] / StatusUp[1]); Clearly you could use better names.
Hmmm, I am not sure that would work - I need to do it in the VB.Net code and the StatusUp is in the Dll in the C# code. I have to somehow pass it to vb.net Something like: ProgressBar.Value = ftpupload.StatusUp(0)/ftpupload.StatusUp(1)
Clearly you'll need to start a new question, this doesn't have anything to do anymore with your old question.
0

It C#, not C++.

converted using http://www.developerfusion.com/tools/convert/csharp-to-vb/

Public Sub UploadData(FTPUri As String, FilePath As String, FileName As String, UserName As String, Password As String)
    'FtpWebRequest reqFTP; 
    StatusUp = New Int64(1) {}

    reqFTP = DirectCast(FtpWebRequest.Create(FTPUri & FileName), FtpWebRequest)
    reqFTP.Method = WebRequestMethods.Ftp.UploadFile
    reqFTP.UseBinary = True
    reqFTP.Credentials = New NetworkCredential(UserName, Password)
    'StreamReader ReadStream = new StreamReader(FilePath); 
    Dim fileInf As New FileInfo(FilePath)

    Dim fs As FileStream = fileInf.OpenRead()

    ' modifyied code 
    Dim bytesSize As Integer = 0
    Dim UpBuffer As Byte() = New Byte(2047) {}

    'reqFTP.ContentLength = ftpResponse.Length; 
    'Stream ftpStream = reqFTP.GetRequestStream(); 
    ftpStream = reqFTP.GetRequestStream()

    bytesSize = fs.Read(UpBuffer, 0, UpBuffer.Length)

    While (InlineAssignHelper(bytesSize, fs.Read(UpBuffer, 0, UpBuffer.Length))) > 0
        'StatusUp[0] = ftpStream.Length; 
        StatusUp(0) = StatusUp(0) + UpBuffer.Length
        StatusUp(1) = fileInf.Length
        ' +startPointInt; 
        ftpStream.Write(UpBuffer, 0, bytesSize)
    End While
    fs.Close()
    ftpStream.Close()
    'response.Close(); 
End Sub

Comments

0
Public Sub UploadData(FTPUri As String, FilePath As String, FileName As String, UserName As String, Password As String)
StatusUp = New Int64(1) {}

reqFTP = DirectCast(FtpWebRequest.Create(FTPUri & FileName), FtpWebRequest)
reqFTP.Method = WebRequestMethods.Ftp.UploadFile
reqFTP.UseBinary = True
reqFTP.Credentials = New NetworkCredential(UserName, Password)

Dim fileInf As New FileInfo(FilePath)

Dim fs As FileStream = fileInf.OpenRead()

' modifyied code
Dim bytesSize As Integer = 0
Dim UpBuffer As Byte() = New Byte(2047) {}

ftpStream = reqFTP.GetRequestStream()

bytesSize = fs.Read(UpBuffer, 0, UpBuffer.Length)

While (InlineAssignHelper(bytesSize, fs.Read(UpBuffer, 0, UpBuffer.Length))) > 0
    StatusUp(0) = StatusUp(0) + UpBuffer.Length
    StatusUp(1) = fileInf.Length
    ' +startPointInt;
    ftpStream.Write(UpBuffer, 0, bytesSize)
End While

fs.Close()
ftpStream.Close()
End Sub

voteup or accept if it works

1 Comment

You don't have to put in "vote up or accept if it works" in all of your answers. If it's sound advice, it will get voted up by the community. By constantly asking for upvotes, you are probably making people "not" vote for your posts. A little bit of an explanation of what your code does to help the questioner makes for better answers, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.