【发布时间】:2019-09-17 18:02:35
【问题描述】:
我有一个将列表中的文件复制到新目录的应用程序。我正在尝试使进度条正常工作,但我的公式导致 System.OverflowException:算术运算导致溢出。我查看了使用文件复制的其他示例,但它们的中心是使用源文件夹除以目标文件夹。挖掘有点不同,因为我使用的是列表中的文件。
感谢您的帮助。
代码
Dim progress = fileCount * 100 / fileList.Count()
BackgroundWorker1.ReportProgress(progress)
获取文件计数的代码
Dim FILE_NAME As String
FILE_NAME = txtFileName.Text
Dim fileNames = System.IO.File.ReadAllLines(FILE_NAME)
fCount = 0
For i = 0 To fileNames.Count() - 1
Dim fileName = fileNames(i)
sFileToFind = location & "\" & fileName & "*.*"
Dim paths = IO.Directory.GetFiles(location, fileName, IO.SearchOption.AllDirectories)
If Not paths.Any() Then
System.IO.File.AppendAllText(orphanedFiles, fileName & vbNewLine)
Else
For Each pathAndFileName As String In paths
If System.IO.File.Exists(pathAndFileName) = True Then
sRegLast = pathAndFileName.Substring(pathAndFileName.LastIndexOf("\") + 1)
Dim toFileLoc = System.IO.Path.Combine(createXMLFldr, sRegLast)
Dim moveToFolder = System.IO.Path.Combine(MoveLocation, "XML files", sRegLast)
'if toFileLoc = XML file exists move it into the XML files folder
If System.IO.File.Exists(toFileLoc) = False Then
System.IO.File.Copy(pathAndFileName, moveToFolder)
System.IO.File.AppendAllText(ListofFiles, sRegLast & vbNewLine)
fCount = fCount + 1
XMLFilename = (sRegLast) + vbCrLf
End If
End If
Next
End If
Dim srcCount = paths.Count()
If (paths.Count() = 0) Then
srcCount = 1
End If
Dim progress = CType(fCount * 100 / srcCount, Integer)
BackgroundWorker1.ReportProgress(progress)
Next
【问题讨论】:
-
你需要检查
fileList.Count = 0是否被0除。 -
@dashier 我添加了一个 if 语句,如果 fileList.Count 为零,则将其更改为一。但我仍然得到 OverflowException
-
您需要显示最少的代码才能重现问题。 不是您的整个程序,但足以让我们看到
fileList是如何创建/填充的。如果您使用调试器并查看值会发生什么?还有,BackgroundWorker1.ReportProgress()要求的类型是什么? -
文件数如何计算?
-
我有一个包含文件列表的文本文件。我将所有行读入 fileList
标签: vb.net progress-bar