Click here to Skip to main content
15,791,440 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to execute an external file and do something when the file was closed
Process. WaitForExit()
Method does not work and return null Exception
please help me.

What I have tried:

Process ExecFile = Process.Start(FilePath);
Updated 3-Aug-22 3:51am

It depends on what is in your FilePath - if it's the name of a data file (.jpg for example) then the process isn't necessarily kicked off by the "run this" request - the system decides what app to run and if there is a single app it may kick it off and return a Process. If it isn't, then a null Process is returned, and you can't wait for it to exit (as the process that is started is a system dialog box).
On my system, a .XLSX will work and open Excel, then wait for an exit, but a .JPG will not, and returns immediately.
Share this answer
This means that ExecFile is null at the time you are calling the WaitForExit() method.
According to Process.Start Method (System.Diagnostics) | Microsoft Docs[^], it could be that
A new Process that is associated with the process resource, or null if no process resource is started.
If the address of the executable file to start is a URL, the process is not started and null is returned.
Now it's up to you to investigate this; please use your debugger for this purpose: put a breakpoint on the process initialization line, start a debug session (F5 on Visual Studio), then execute line-by-line (F11) and watch carefully for the values held by your variables. We cannot do that for you.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900