1

Iam trying to run a .exe file with argument from unity using C#, this is what I came up with so far;

System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
process.StartInfo.FileName = "C://PATH//ABC.exe";
process.StartInfo.Arguments = "ARGUMENT";
process.Start();
process.WaitForExit();

The argument should create a text file, but nothing is happening.

And this error keeps on showing up :

 NullReferenceException: Object reference not set to an instance of an object
 System.Diagnostics.Process.Start_common (System.Diagnostics.ProcessStartInfo startInfo, 
System.Diagnostics.Process process) System.Diagnostics.Process.Start () 
(wrapper remoting-invoke-with-check) System.Diagnostics.Process:Start ()

Can anyone tell me why is the text file not creating, and how can I fix this error?

Ran
  • 515
  • 2
  • 8
  • 20
  • 4
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – TJ Wolschon Apr 17 '18 at 18:39
  • Do you have more of the callstack? It looks a bit like this problem https://bugzilla.xamarin.com/show_bug.cgi?id=38544. On which OS/CPU are you running this? If you leave out the WaitForExit() does it work then? – rene Apr 17 '18 at 18:52
  • 1
    Can you try to set [`UseShellExecute`](https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.useshellexecute(v=vs.110).aspx) on the StartInfo to `false`? – rene Apr 17 '18 at 18:55
  • That didn't work unfortunatly – Ran Apr 18 '18 at 12:29

1 Answers1

0

try adding start info before setting the values:

process.StartInfo = System.Diagnostics.ProcessStartInfo();

like so:

System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo = System.Diagnostics.ProcessStartInfo();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
process.StartInfo.FileName = "C://PATH//ABC.exe";
process.StartInfo.Arguments = "ARGUMENT";
process.Start();
process.WaitForExit();

hope that helps

Dean Van Greunen
  • 1,525
  • 12
  • 22