简体   繁体   English

在C#中通过批处理文件运行exe时,为Null异常,但在手动单击批处理时则为空

[英]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. 我们有一个专为我们打造的第三方exe。 It reads word docs and turns them into XML so we can process them. 它读取word文档并将其转换为XML,以便我们对其进行处理。

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: 它只是循环遍历我们doc文件夹中的所有文档,并将参数传递给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 但是,当我们尝试在C#代码中调用它时,它为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. 当尝试手动和以编程方式在命令提示符下运行exe时,我也得到了相同的结果。 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();

您需要执行的文件是“ CMD.exe”,而要运行的批处理文件是您的参数:-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM