Need Code to delete a file if file size is larger than 2 MBs
I have a watched folder for incoming pdf files. I need VB.Net code to delete any incoming file if the file size is larger than 2 MBs.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Kirtan PatelRank #292 · 8.6K reputationPosted Dec 17, 2009, 9:00 AM EST
Dim filename As String = "D:\Songs\lets.mp3" Dim f As New FileInfo(filename) Dim FileSize As Long = f.Length / (1024 * 1024) If FileSize > 2 Then File.Delete(filename) End IfHiren SoniRank #694 · 3.5K reputationPosted Dec 16, 2009, 7:09 AM EST
Tony Wallace3 reputationPosted Dec 16, 2009, 7:28 AM EST
Hirendra SisodiyaRank #258 · 9.8K reputationPosted Dec 17, 2009, 6:40 PM EST
Dim MyFile As New IO.FileInfo("C:\test.pdf")
Dim FileSize As Double = MyFile.Length / (1024 * 1024) 'Find file Size in Mb
If FileSize > 2 Then 'check test.pdf size is greter than 2 mb or not
MyFile.Delete() 'Delete file
End If
thank you