在 VB.Net 中使用 C# 上传函数

本文关键字:函数 VB Net | 更新日期: 2023-09-27 18:19:07

这是非常具体的。 我需要有关在 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();
}

在我的 vb.net 程序中,我是这样称呼它的:

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

它工作正常,除了完成后它短 2 个字节。 我不知道足够的 C# 来确定 C# 代码是否错误(我没有写它,而且写的人已经离开了公司(,但在某个地方似乎它没有关闭文件或其他东西。

有什么想法吗?

在 VB.Net 中使用 C# 上传函数

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

这里有两个危险信号。 第一个是问题的原因,额外的fs。在进入 while 循环之前调用 Read((。 这是您不使用和不上传的 2048 字节。 只需删除该行即可。 通过使用 for (;;) 和中断来避免此类错误。

StatusUp[0] 赋值看起来很糟糕,你应该添加 bytesSize,而不是 UpBuffer.Length。 这有什么副作用并不明显。

它是C#,而不是C++。

使用 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

投票或接受(如果有效(