vb.net - Delete multiple files -
i'm trying delete multiple files in same folder vb.net, haven't succeed yet. please?
i tried
dim filetodelete1 string dim filetodelete2 string dim filetodelete3 string dim filetodelete4 string dim filetodelete5 string filetodelete1 = application.startuppath & "\1.exe" filetodelete2 = application.startuppath & "\2.dll" filetodelete3 = application.startuppath & "\3.dll" filetodelete4 = application.startuppath & "\4.dll" filetodelete5 = application.startuppath & "\5.dll" if system.io.file.exists( filetodelete1 ) = true my.computer.filesystem.deletefile( filetodelete1 ) elseif system.io.file.exists( filetodelete2 ) = true my.computer.filesystem.deletefile( filetodelete2 ) elseif system.io.file.exists( filetodelete3 ) = true my.computer.filesystem.deletefile( filetodelete3 ) elseif system.io.file.exists( filetodelete4 ) = true my.computer.filesystem.deletefile( filetodelete4 ) elseif system.io.file.exists( filetodelete5 ) = true my.computer.filesystem.deletefile( filetodelete5 ) end if
several problems here.
first, file.exists
returns boolean value. "=true"
unnecessary because you're asking if true=true
. fortunately, is.
second, file existence or not it's not way fail. instance, if file in use, you'll exception. should handle it.
third, if need delete thousand files? create string
each 1 of them? there better options, instance, getfiles
method return readonly list of strings
, each 1 representing 1 file. don't know needs, catch files mention, following call can made:
fileio.filesystem.getfiles(application.startuppath, fileio.searchoption.searchtoplevelonly, {"?.exe", "?.dll"})
it every exe , dll file if it's name consists in 1 character.
finally, notice if first condition met, no other evaluated, hence no other file deleted. implementation you'll need run program 5 times in order delete every file. getfiles method solves well.
additionally, consider importing namespaces don't need prefix them in every method call.
Comments
Post a Comment