简体   繁体   English

在VB.Net中使用C#上传功能

[英]Using C# Upload function in VB.Net

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: 我需要在VB.Net程序中使用C#函数的帮助,该函数位于DLL中,并且代码如下:

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: 在我的vb.net程序中,我这样称呼它:

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. 可以正常工作,只是完成后它只有2个字节。 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. 我对C#的了解不足,无法弄清楚C#代码是否错误(我没有写出来,而是那位离开公司的人),但是似乎某个地方没有关闭文件或其他东西。

Any ideas? 有任何想法吗?

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. 第一个是导致问题的原因,在进入while循环之前需要执行额外的fs.Read()调用。 That's 2048 bytes you don't use and don't upload. 那是您不使用也不上传的2048字节。 Just delete that line. 只需删除该行。 You avoid these kind of bugs by using for (;;) and break. 通过使用for(;;)和break可以避免这类错误。

The StatusUp[0] assignment looks bad, you should add bytesSize, not UpBuffer.Length. StatusUp [0]分配看起来很糟糕,应该添加bytesSize而不是UpBuffer.Length。 It isn't otherwise obvious what side-effects that has. 否则,没有什么副作用。

It C#, not C++. 它是C#,而不是C ++。

converted using http://www.developerfusion.com/tools/convert/csharp-to-vb/ 使用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
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 投票或接受(如果可行)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM