.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

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -