VB.NET算术运算导致使用FileStream的溢出
我正试图使用VB.NET算术运算导致使用FileStream的溢出,vb.net,filestream,Vb.net,Filestream,我正试图使用文件流将文件从一个目录复制到另一个目录,它工作正常,但在复制大文件时,它会抛出一个错误算术运算导致溢出。它无法复制大于20MB的文件。拜托,我是编程新手,你能帮我吗 Private mCurrentFile As String = String.Empty Public ReadOnly Property CurrentFile() As String Get Return mCurrentFile End Get End Property Dim
文件流将文件从一个目录复制到另一个目录,它工作正常,但在复制大文件时,它会抛出一个错误算术运算导致溢出。
它无法复制大于20MB的文件。拜托,我是编程新手,你能帮我吗
Private mCurrentFile As String = String.Empty
Public ReadOnly Property CurrentFile() As String
Get
Return mCurrentFile
End Get
End Property
Dim FS As FileStream
mCurrentFile = GetFileName(URL)
Dim wRemote As WebRequest
Dim bBuffer As Byte()
ReDim bBuffer(256)
Dim iBytesRead As Integer
Dim iTotalBytesRead As Integer
FS = New FileStream(Location, FileMode.Create, FileAccess.Write)
wRemote = WebRequest.Create(URL)
Dim myWebResponse As WebResponse = wRemote.GetResponse
RaiseEvent FileDownloadSizeObtained(myWebResponse.ContentLength)
Dim sChunks As Stream = myWebResponse.GetResponseStream
Do
iBytesRead = sChunks.Read(bBuffer, 0, 256)
FS.Write(bBuffer, 0, iBytesRead)
iTotalBytesRead += iBytesRead
If myWebResponse.ContentLength < iTotalBytesRead Then
RaiseEvent AmountDownloadedChanged(myWebResponse.ContentLength)
Else
RaiseEvent AmountDownloadedChanged(iTotalBytesRead)
End If
Loop While Not iBytesRead = 0 And dStop = False
sChunks.Close()
FS.Close()
Private Sub _Downloader_FileDownloadSizeObtained(ByVal iFileSize As Long) Handles _Downloader.FileDownloadSizeObtained
'FIRES WHEN WE HAVE GOTTEN THE DOWNLOAD SIZE, SO WE KNOW WHAT BOUNDS TO GIVE THE PROGRESS BAR
ProgressBar1.Value = 0
ProgressBar1.Maximum = CInt(iFileSize)
End Sub
Private mCurrentFile As String=String.Empty
作为字符串的公共只读属性CurrentFile()
得到
返回mCurrentFile
结束
端属性
将FS设置为文件流
mCurrentFile=GetFileName(URL)
Dim wRemote作为WebRequest
Dim bBuffer作为字节()
ReDim bBuffer(256)
Dim iBytes读取为整数
Dim ITOTALBYTES读取为整数
FS=新文件流(位置,FileMode.Create,FileAccess.Write)
wRemote=WebRequest.Create(URL)
将myWebResponse设置为WebResponse=wRemote.GetResponse
RaiseEvent文件DownloadSizeObtained(myWebResponse.ContentLength)
Dim sChunks As Stream=myWebResponse.GetResponseStream
做
iBytesRead=sChunks.Read(bBuffer,0,256)
FS.Write(bBuffer,0,iBytesRead)
iTotalBytesRead+=iBytesRead
如果myWebResponse.ContentLength
错误在这里抛出
Private Sub _Downloader_AmountDownloadedChanged(ByVal iNewProgress As Long) Handles _Downloader.AmountDownloadedChanged
'FIRES WHEN MORE OF THE FILE HAS BEEN DOWNLOADED
ProgressBar1.Value = Convert.ToInt64(iNewProgress) '<-------------- Error throws here
Application.DoEvents()
End Sub
Private Sub\u Downloader\u AmountDownloadedChanged(ByVal iNewProgress作为Long)句柄\u Downloader.AmountDownloadedChanged
'在下载更多文件时激发
ProgressBar1.Value=Convert.ToInt64(iNewProgress)'首先,为什么要将Long
传递给Convert.ToInt64
?它已经是Int64
。这就是长码。至于例外情况,什么类型是UserControl12.Value
?我猜它是Integer
,即Int32
。这意味着,如果您的Long
包含大于Int32.MaxValue
的值,则尝试将其强制为Int32
将导致溢出,即将有太多数据无法容纳。看起来您有选项Strict Off
或该代码甚至无法编译,因为它会拒绝您尝试将Long
值放入Integer
属性中。对于此项目和您处理的每个其他项目,您应该启用选项。它不会修复这样的问题,但会提醒您它们确实存在。如果您试图在值为Long
时显示进度,那么最好的选择是使用这两个Long
值来计算百分比,然后将其用作进度,而不是绝对值。那么问题与我的情况完全一样已经声明了,那么为什么你还没有修复它,而不是让我再看一遍呢?项目属性->编译选项卡;要使其始终成为默认值(推荐):工具->选项->项目和解决方案->VB默认值