'How do I start a application and close it immediately in c#

As part of a automated test case, I have to start an application(for example notepad) and close it without any user intervention. The executable I use to launch this application should be called in System account and to achieve this I use PSExec utility. So the call looks somewhat like this :

ProcessStartInfo startInfo = new ProcessStartInfo();
 startInfo.CreateNoWindow = true;
            startInfo.UseShellExecute = false;
            startInfo.FileName = "PsExec.exe";
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.Arguments = " -i -s " + myExecutable.exe + " -application notepad.exe";
            Process exeProcess = Process.Start(startInfo)
exeProcess.WaitForExit();

The above code starts notepad and waits until it is closed, only then will I get a proper exit code from myExecutable.exe

Some of the ways that I tried :

Using timer with WaitForExit and Kill, this doesn't close notepad but exists the program with 0 and -1 respectively, maybe because I'm killing the process PSExec and not myexecutable.

Is there a way to close Notepad.exe automatically? Or even an API that can be used to run a process in System account(So I can get rid of PSExec entirely).



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source