简体   繁体   English

c#中的批处理文件执行

[英]batch file execution in c#

I am running a Java batch file from C#. 我正在从C#运行Java批处理文件。 If I run it by double clicking it executes successfully, but if I run it from C# code it gives exception in thread 如果我通过双击它运行它成功执行,但如果我从C#代码运行它,它在线程中给出异常

"exception in "main" thread java.lang.noclassdeffoundError".. “主”线程java.lang.noclassdeffoundError“中的异常”..

what can be the reason and how can it be solved? 可能是什么原因以及如何解决? I am using the code: 我正在使用代码:

var si = new ProcessStartInfo();

si.CreateNoWindow = true;
si.FileName = "batch-file path";
si.UseShellExecute = true;

Process.Start(si);

您很可能遗漏了系统环境变量中包含的一些参数。

Try setting working directory like this 尝试像这样设置工作目录

process.StartInfo.WorkingDirectory = "C:\";

Also, try few other options as mentioned here, 另外,尝试这里提到的其他几个选项,

http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/20992653-dabf-4b31-85f7-e7bfbfb4557c http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/20992653-dabf-4b31-85f7-e7bfbfb4557c

尝试将以下代码添加为批处理文件的第一行。

@cd /d %~dp0

Do not use batch_process_path + "\\" + instead use Path.Combine() to make sure the path is correctly fitted with slashes. 不要使用batch_process_path +“\\”+而是使用Path.Combine()来确保路径正确配有斜杠。

Also read this "When UseShellExecute is true, the WorkingDirectory property specifies the location of the executable" 另请阅读 “当UseShellExecute为true时,WorkingDirectory属性指定可执行文件的位置”

So set it to false. 所以把它设置为假。

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

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