.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

How to check data type in C++? -

javascript - Is getTimezoneOffset() stable during daylight saving transition? -

sql server - SQL Query returns one result, does not return 0 or NULL result -