.net - How to eliminate for each loop? -
i using following code excel.exe starting time. (following code okey)
each prog process in system.diagnostics.process.getprocessesbyname("excel") label100.text = prog.starttime.tostring next
but need more professional code.
so, how eliminate each loop?
best wishes.
you can store result of process.getprocessesbyname
, check if it's length >= 1
:
dim excelprocs = system.diagnostics.process.getprocessesbyname("excel") if excelprocs.length >= 1 label100.text = excelprocs(0).starttime.tostring() else messagebox.show("error") end if
or use enumerable.firstordefault
linq method:
dim excelproc = system.diagnostics.process.getprocessesbyname("excel").firstordefault()
then can check if excelproc isnot nothing
before use it:
if excelproc isnot nothing label100.text = excelproc.starttime.tostring() else messagebox.show("error") end if
Comments
Post a Comment