我有以下代码,我发现是VB.NET,我想稍微修改它,所以当它获取PNG图像时,它从文件夹中选择最大的文件大小。任何帮助非常感谢!
Imports system.data
imports system.io
imports system.windows.forms
Imports system.drawing
Private Sub xtraReport1_BeforePrint(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintEventArgs)
Dim ReportDS As system.data.dataset = CType(Report.DataSource,
System.Data.DataSet)
if CType(Report.DataSource, DataSet).Tables("Data").Rows.Count = 0 then
Exit Sub
End if
if CType(Report.DataSource,
DataSet).Tables("Data").columns.contains("CourierLabel") = false then
CType(Report.DataSource, DataSet).Tables("Data").columns.add("CourierLabel",
gettype(String))
end if
Dim ExportPath as string = SystemBusiness.Executescalarstr("select
cod_cdd_export_path from courier_detail where cod_id = " &
CType(Report.DataSource, DataSet).Tables("Data").rows(0).item("dh_cod_id"))
Dim DelNumber as string = CType(Report.DataSource,
DataSet).Tables("Data").rows(0).item("dh_number").tostring
Dim DelDate as string = cdate(CType(Report.DataSource,
DataSet).Tables("Data").rows(0).item("dh_datetime")).tostring("yyyy-MM-dd")
Dim DirPath as string = System.IO.Path.Combine(ExportPath, DelDate,
DelNumber)
'DirPath = "C:\temp"
For each f as string In Directory.GetFiles(DirPath)
if f.contains("png") Then
CType(Report.DataSource,
DataSet).Tables("Data").rows(0).item("CourierLabel") =
System.IO.Path.Combine(DirPath, f)
'messagebox.show(System.IO.Path.Combine(DirPath, f))
'system.windows.forms.messagebox.show(System.IO.Path.Combine(DirPath, f))
PictureBox1.Image = Image.FromFile(System.IO.Path.Combine(DirPath, f))
picturebox1.image.rotateflip(RotateFlipType.Rotate90FlipNone)
end if
Next
End Sub
答案 0 :(得分:1)
基础:(
我可以提供方法:
首先找到最大的文件: maxFileSize = 0; 循环文件并将每个文件大小与maxFileSize进行比较,如果更大则保存filePath。 在循环之后,您将获得目录中最大文件的路径。
Dim maxFileSize = 0
Dim filePath = Nothing
For each f as string In Directory.GetFiles(DirPath)
if f.Size > maxFileSize Then
maxFileSize = f.Size
filePath = f.FullPath
end if
Next
我不确定f.Size和f.FullPath,但这应该很容易在调试器中解决;)