Josh Blade
Josh Blade

Reputation: 981

Null Exception when running exe via batch file in C#, but not when clicking the batch manually

We have a 3rd party exe that was built for us. It reads word docs and turns them into XML so we can process them.

We have a batch file that runs fine when clicked. It just loops over all the docs in our doc folder and passes the parameters to the WordExtract.exe:

for %%a in (MyPATH\*.doc) do WordExtract.exe -InputFile="%%a" -TempFile0="%%a.html" -OutputFile= "%%a.xml"

When we try to call it in our C# code however, it gives us a null reference exception for the WordExtract.exe

System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  Source=WordExtract
  StackTrace:
       at WordExtract.Converter.word2text(FileInfo file, FileInfo txtfile)
       at WordExtract.Program.Main(String[] args)
  InnerException:

So it's calling the batchfile fine. I'm just not sure why the batchfile produces no errors when I run it manually, but somehow has a null exception when run Programatically.

I also had the same result when trying to just run the exe in command prompt manually and programatically. Works fine when done manually and gives the above error when run programatically.

Any ideas on this? Running the batch file should produce the same results either way. It doesn't take any inputs at all so I can't be screwing that up programatically.

Here's the code for running my batch file:

Process proc = new Process();
            string targetDir = string.Format(@"C:\BatchTest");//this is where batch.bat is
            proc.StartInfo.WorkingDirectory = targetDir;
            proc.StartInfo.FileName = "batch.bat";
            proc.StartInfo.CreateNoWindow = false;
            proc.Start();
            proc.WaitForExit();

Upvotes: 0

Views: 1150

Answers (1)

Derek
Derek

Reputation: 8628

The file that you need to Execute is "CMD.exe" and the batch file you want to run is your argument :-

System.Diagnostics.Process.Start("cmd", "/c C:\\BatchTest\\batch.bat");

Upvotes: 1

Related Questions