OpenFileDialog控件是否限制了它可以上传的最大文件数? 我正忙着开发一个将文件上传到FTP服务器的应用程序。这些代码可以完美地上传多达80个文件,但如果我尝试上传超过80个文件,则会显示"找不到文件"。 我想修改代码,以便它可以接受任何数量和大小的文件。如果OpenFileDialog无法实现,请告诉我一个替代方案。谢谢 这是我的代码:
Imports System.IO
Imports System.Net
Imports System.Windows.Forms
Public Class FTPServerApp
Private Sub FTPServerApp_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
End Sub
Private Sub btnAddFile_Click(sender As Object, e As EventArgs) Handles btnAddFile.Click
Dim OpenFile As New OpenFileDialog() With {.Multiselect = True}
If OpenFile.ShowDialog() = DialogResult.OK Then
If OpenFile.FileNames.Count = 1 Then
Dim newItm As New ListViewItem(Path.GetFileName(OpenFile.FileName))
newItm.SubItems.Add(txtServer.Text & "/" & Path.GetFileName(OpenFile.FileName))
newItm.SubItems.Add("")
newItm.SubItems.Add(OpenFile.FileName)
lstCollection.Items.Add(newItm)
ElseIf OpenFile.FileNames.Count > 1 Then
For Each Str As String In OpenFile.FileNames
If My.Computer.FileSystem.FileExists(Str) Then
Dim newItm As New ListViewItem(Path.GetFileName(Str))
newItm.SubItems.Add(txtServer.Text & "/" & Path.GetFileName(Str))
newItm.SubItems.Add("")
newItm.SubItems.Add(Str)
lstCollection.Items.Add(newItm)
End If
Next
End If
lblCount.Text = "0 /" & lstCollection.Items.Count
End If
End Sub
Private Sub btnUploadFile_Click(sender As Object, e As EventArgs) Handles btnUploadFile.Click
bWorker.RunWorkerAsync()
End Sub
Private Sub bWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bWorker.DoWork
For Each itm As ListViewItem In lstCollection.Items
Try
Dim request As FtpWebRequest = DirectCast(WebRequest.Create(New Uri(itm.SubItems.Item(1).Text)), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.UploadFile
request.Credentials = New NetworkCredential(txtUser.Text, txtPassword.Text)
request.UseBinary = True
request.UsePassive = False
Dim buffer(1023) As Byte
Dim bytesIn As Long = 1
Dim totalBytesIn As Long = 0
Dim filepath As System.IO.FileInfo = New System.IO.FileInfo(itm.SubItems.Item(3).Text)
Dim ftpstream As System.IO.FileStream = filepath.OpenRead()
Dim flLength As Long = ftpstream.Length
Dim reqfile As System.IO.Stream = request.GetRequestStream
Do Until bytesIn < 1
bytesIn = ftpstream.Read(buffer, 0, 1024)
If bytesIn > 0 Then
reqfile.Write(buffer, 0, bytesIn)
totalBytesIn += bytesIn
If flLength > 0 Then
Dim perc As Integer = (totalBytesIn / flLength) * 100
bWorker.ReportProgress(perc, CInt(itm.Index + 1))
End If
End If
Loop
reqfile.Close()
ftpstream.Close()
itm.SubItems.Item(2).Text = "File Succesfully uploaded!"
Catch ex As Exception
itm.SubItems.Item(2).Text = "File was not uploaded!"
End Try
Next
End Sub
Private Sub bWorker_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles bWorker.ProgressChanged
pBar.Value = e.ProgressPercentage
lblPercent.Text = e.ProgressPercentage & " %"
lblCount.Text = CInt(e.UserState) & " / " & lstCollection.Items.Count
End Sub
Private Sub bWorker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bWorker.RunWorkerCompleted
MsgBox("Finish!")
End Sub
End Class